gpt4 book ai didi

删除 R 中至少包含一个负值的所有行

转载 作者:行者123 更新时间:2023-12-05 00:45:22 25 4
gpt4 key购买 nike

我不想删除所有负面元素,我可以做到。但是,我想删除所有行,即使它在我的数据框中只包含一个负值。

> head(kosoyCorrected)
BER1_EW BER2_EW BER3_EW BER4_EW BER5_EW BER6_EW
1 7.087613184 7.09928796 7.087194381 6.96315939 7.086734346 7.09934523
2 4.599450934 3.89325300 4.160360141 4.81419817 4.090161726 4.34070903
3 0.100477184 0.02351617 -0.001589346 0.01072809 0.023073244 -0.06953596
4 0.132531627 0.09994992 0.123564389 0.13849246 0.217604484 0.09164854
5 -0.005220038 0.07117798 0.133075865 0.05525490 -0.003944601 0.10597363
6 0.107204375 0.11755171 0.060868101 0.14361525 0.109494893 0.13081894

例如,在这里,我想删除整个第 3 行和第 5 行,而不仅仅是这些行中的 4 个负值。

提前致谢!

最佳答案

我们可以使用 rowSums 来创建一个逻辑向量来对行进行子集化

subset(kosoyCorrected, !rowSums(kosoyCorrected < 0))
# BER1_EW BER2_EW BER3_EW BER4_EW BER5_EW BER6_EW
#1 7.0876132 7.09928796 7.0871944 6.9631594 7.0867343 7.09934523
#2 4.5994509 3.89325300 4.1603601 4.8141982 4.0901617 4.34070903
#4 0.1325316 0.09994992 0.1235644 0.1384925 0.2176045 0.09164854
#6 0.1072044 0.11755171 0.0608681 0.1436152 0.1094949 0.13081894

或者另一个选项是Reduce

subset(kosoyCorrected, Reduce(`&`, lapply(kosoyCorrected, `>`, 0)))

dplyr 中带有 filter_all

的矢量化选项
library(dplyr)
kosoyCorrected %>%
filter_all( all_vars(. > 0))
# BER1_EW BER2_EW BER3_EW BER4_EW BER5_EW BER6_EW
#1 7.0876132 7.09928796 7.0871944 6.9631594 7.0867343 7.09934523
#2 4.5994509 3.89325300 4.1603601 4.8141982 4.0901617 4.34070903
#4 0.1325316 0.09994992 0.1235644 0.1384925 0.2176045 0.09164854
#6 0.1072044 0.11755171 0.0608681 0.1436152 0.1094949 0.13081894

或在较新的版本中 across

kosoyCorrected %>% 
filter(across(everything(), ~ . > 0))

数据

kosoyCorrected <- structure(list(BER1_EW = c(7.087613184, 4.599450934, 0.100477184, 
0.132531627, -0.005220038, 0.107204375), BER2_EW = c(7.09928796,
3.893253, 0.02351617, 0.09994992, 0.07117798, 0.11755171), BER3_EW = c(7.087194381,
4.160360141, -0.001589346, 0.123564389, 0.133075865, 0.060868101
), BER4_EW = c(6.96315939, 4.81419817, 0.01072809, 0.13849246,
0.0552549, 0.14361525), BER5_EW = c(7.086734346, 4.090161726,
0.023073244, 0.217604484, -0.003944601, 0.109494893), BER6_EW = c(7.09934523,
4.34070903, -0.06953596, 0.09164854, 0.10597363, 0.13081894)),
class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6"))

关于删除 R 中至少包含一个负值的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63784412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com