[英]Selecting rows if all values match criteria in R
I have a data frame in R
called records
.我在
R
有一个名为records
的数据框。 Here is an extract of that dataframe:这是该数据帧的摘录:
head(records[430:438,12:16])
2B2_pyr_hor 2B3_pyr_hor 3A1_pyr_hor 3A2_pyr_hor 3A3_pyr_hor
3760 22.45732 21.63635 24.36742 20.11058 28.10608
3761 33.81917 31.96332 36.45907 29.76239 41.94274
3762 47.38274 44.32909 51.10520 41.49609 57.54125
3763 62.47752 58.20380 67.02381 54.71055 74.55541
3764 78.17504 73.55406 83.88879 69.30802 92.57984
3905 90.84730 94.72380 89.43837 106.69484 81.44858
I want to extract all the rows where all the values are <100
but when I do records=records[records<100,]
, it seems I get all the rows where at least one value is less <100
.我想提取所有行,所有的值都
<100
,但是当我做records=records[records<100,]
看来我得到的所有的行,其中至少一个值小于<100
。
So in this example, I do not want row with index=3905
yet I still get it.所以在这个例子中,我不想要
index=3905
行,但我仍然得到它。 (The above extract is after I applied records=records[records<100,]
) (以上摘录是在我应用后
records=records[records<100,]
)
Nothing in my first past searches have produced any useful solutions unfortunately.不幸的是,我过去的第一次搜索没有产生任何有用的解决方案。 It must be something simple that I am missing somewhere?
一定是我在某个地方遗漏了一些简单的东西?
We can use rowSums
我们可以使用
rowSums
df[rowSums(df < 100) == ncol(df), ]
#Alternative version :
#df[rowSums(df >= 100) == 0, ]
# 2B2_pyr_hor 2B3_pyr_hor 3A1_pyr_hor 3A2_pyr_hor 3A3_pyr_hor
#3760 22.5 21.6 24.4 20.1 28.1
#3761 33.8 32.0 36.5 29.8 41.9
#3762 47.4 44.3 51.1 41.5 57.5
#3763 62.5 58.2 67.0 54.7 74.6
#3764 78.2 73.6 83.9 69.3 92.6
Or with lapply
and Reduce
或者使用
lapply
和Reduce
df[Reduce(`&`, lapply(df, `<`, 100)), ]
data数据
df <- structure(list(`2B2_pyr_hor` = c(22.45732, 33.81917, 47.38274,
62.47752, 78.17504, 90.8473), `2B3_pyr_hor` = c(21.63635, 31.96332,
44.32909, 58.2038, 73.55406, 94.7238), `3A1_pyr_hor` = c(24.36742,
36.45907, 51.1052, 67.02381, 83.88879, 89.43837), `3A2_pyr_hor` = c(20.11058,
29.76239, 41.49609, 54.71055, 69.30802, 106.69484), `3A3_pyr_hor` = c(28.10608,
41.94274, 57.54125, 74.55541, 92.57984, 81.44858)), class = "data.frame",
row.names = c("3760","3761", "3762", "3763", "3764", "3905"))
You can use apply
and all
to select rows where all values are below 100 .您可以使用
apply
和all
来选择所有值都低于 100 的行。
records[apply(records<100, 1, all),]
# X2B2_pyr_hor X2B3_pyr_hor X3A1_pyr_hor X3A2_pyr_hor X3A3_pyr_hor
#3760 22.45732 21.63635 24.36742 20.11058 28.10608
#3761 33.81917 31.96332 36.45907 29.76239 41.94274
#3762 47.38274 44.32909 51.10520 41.49609 57.54125
#3763 62.47752 58.20380 67.02381 54.71055 74.55541
#3764 78.17504 73.55406 83.88879 69.30802 92.57984
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.