user1172468
user1172468

Reputation: 5464

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):

> 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. 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")

Upvotes: 1

Views: 30

Answers (1)

akrun
akrun

Reputation: 887501

We can get the rowSums of the logical matrix (is.na(d)), use that to create a logical vector (..<2) to subset the rows.

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

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

Upvotes: 1

Related Questions