简体   繁体   English

根据行中有多少个元素过滤R中的数据帧

[英]filtering a dataframe in R based on how many elements in a Row are filled out

I have the following data frame (dput at end): 我有以下数据框(末尾为put):

> d
   a  b  d
1  1 NA NA
2 NA NA NA
3  2  2  2
4  3  3 NA

I want to filter the rows that have at least two items that are not NA. 我要过滤的行中至少有两个不是NA的项目。 I wish to get the result -- how do I do that?: 我希望得到结果-我该怎么做?:

> d
   a  b  d
3  2  2  2
4  3  3 NA




> dput(d)

structure(list(a = c(1, NA, 2, 3), b = c(NA, NA, 2, 3), d = c(NA, 

NA, 2, NA)), .Names = c("a", "b", "d"), row.names = c(NA, -4L

), class = "data.frame")

We can get the rowSums of the logical matrix ( is.na(d) ), use that to create a logical vector ( ..<2 ) to subset the rows. 我们可以得到rowSums逻辑矩阵(的is.na(d)用它来创建逻辑矢量( ..<2到子集的行。

d[rowSums(is.na(d))<2,]
#  a b  d
#3 2 2  2
#4 3 3 NA

Or as @DavidArenburg mentioned, it can be also done with Reduce 或者就像@DavidArenburg提到的那样,也可以使用Reduce来完成

df[Reduce(`+`, lapply(df, is.na)) < 2, ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM