繁体   English   中英

dplyr 过滤器基于跨列和列内的条件

[英]dplyr filter based on conditions across and within column

我想验证调查响应,包括根据列内和跨列的条件删除带有 NA 的行。 下面的示例数据集

col1 <- c("Yes", "Yes", "No", "No", NA)
col2 <- c("Yes", NA, "No", NA, NA)
col3 <- c("No", "Yes", "No", NA, NA)

dataset <- data.frame(col1, col2, col3)
dataset

所需的输出涉及过滤掉 col1 中的所有行,然后仅删除 col1 中为 Yes 且任何其他列中为 NA 的行。 所需的输出低于`

  col1 col2 col3
1  Yes  Yes   No
2   No   No   No
3   No <NA> <NA>

` 我试过基本的过滤操作,比如

dataset %>% filter(col1 == "Yes" | !is.na(.)) 

与其他运算符,如“&、|” 但没有运气,我不确定如何在此处应用 across 或 filter_if 以使其工作。 我认识到这与https://stackoverflow.com/questions/43938863/dplyr-filter-with-condition-on-multiple-columns非常相似,但不同之处足以保证再次问这个问题。

我在这里错过了什么?

您的逻辑封装有:

dataset %>%
  filter(!(is.na(col1) | (col1 == "Yes" & (is.na(col2) | is.na(col3)))))
#>   col1 col2 col3
#> 1  Yes  Yes   No
#> 2   No   No   No
#> 3   No <NA> <NA>

我们可以用缩进和注释重写它,使逻辑更清晰:

dataset %>%
  filter(!(                       # Remove any of the following cases:
      is.na(col1)                       # Column 1 is missing
      |                               # OR 
      (col1 == "Yes"                    # col1 is yes               
       &                                # AND
      (is.na(col2) | is.na(col3))       # Either col2 OR col3 are missing
      )
 ))
#>   col1 col2 col3
#> 1  Yes  Yes   No
#> 2   No   No   No
#> 3   No <NA> <NA>

可以使用if_any来处理第二个过滤条件:

dataset %>% 
  filter(complete.cases(col1), 
         !(col1 == "Yes" & if_any(-col1, is.na)))

  col1 col2 col3
1  Yes  Yes   No
2   No   No   No
3   No <NA> <NA>

暂无
暂无

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

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