我们可以使用across
对' type ','company'列进行循环,并返回在指定列中没有任何NA的行。
library(dplyr)
df %>%
filter(across(c(type, company), ~ !is.na(.)))
使用filter
时,有两个选项与filter_at/filter_all
中使用的all_vars/any_vars
类似。
df %>%
filter(if_any(c(company, type), ~ !is.na(.)))
或使用if_all
:Or using
if_all
df %>%
filter(!if_all(c(company, type), is.na))
id type company
1 2 <NA> ADM
2 3 North Alex
3 4 South <NA>
4 NA North BDA
5 6 <NA> CA
数据
df <- structure(list(id = c(1L, 2L, 3L, 4L, NA, 6L), type = c(NA, NA,
"North", "South", "North", NA), company = c(NA, "ADM", "Alex",
NA, "BDA", "CA")), class = "data.frame", row.names = c(NA, -6L
))