简体   繁体   English

R 中 dplyr 的过滤器中的 function 参数

[英]function argument in filter of dplyr in R

filter(data, function(x) sum(is.na(x))>2)

I want to use this piece of code to get a subset of data which contains rows involve less than 2 NA values, however, the error occurs:我想使用这段代码来获取包含少于 2 个 NA 值的行的数据子集,但是会发生错误:

Error: Argument 2 filter condition does not evaluate to a logical vector错误:参数 2 过滤条件不计算为逻辑向量

Just wondering the reason and how could I deal with it?只是想知道原因,我该如何处理? Many thanks!非常感谢!

We can use filter with rowSums我们可以将filterrowSums一起使用

library(dplyr)
data %>%
   filter(rowSums(is.na(.)) < 2)

Or using base R或使用base R

data[rowSums(is.na(data)) < 2,]

data数据

data <- data.frame(col1 = c(2, 3, NA), col2 = c(2, NA, NA), col3 = c(1, 2, 3))

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

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