简体   繁体   中英

Filter rows with dplyr/magrittr based on entire row

One is able to filter rows with dplyr with filter , but the condition is usually based on specific columns per row such as

d <- data.frame(x=c(1,2,NA),y=c(3,NA,NA),z=c(NA,4,5))
d %>% filter(!is.na(y))

I want to filter the row by whether the number of NA is greater than 50%, such as

d %>% filter(mean(is.na(EACHROW)) < 0.5 )

How do I do this in a dplyr/magrittr flow fashion?

You could use rowSums or rowMeans for that. An example with the provided data:

> d
   x  y  z
1  1  3 NA
2  2 NA  4
3 NA NA  5

# with rowSums:
d %>% filter(rowSums(is.na(.))/ncol(.) < 0.5)

# with rowMeans:
d %>% filter(rowMeans(is.na(.)) < 0.5)

which both give:

  x  y  z
1 1  3 NA
2 2 NA  4

As you can see row 3 is removed from the data.


In base R, you could just do:

d[rowMeans(is.na(d)) < 0.5,]

to get the same result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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