简体   繁体   中英

How to use dplyr across to filter NA in multiple columns

I am trying to filter out rows with NA values across multiple columns. A row should only be dropped if all columns of interest are NA.

The scenario is the same as in this question (but I don't have enough reputation to make a comment): filtering data frame based on NA on multiple columns

One of the solutions is to use:

library(dplyr)
df_non_na <- df %>% filter_at(vars(type,company),all_vars(!is.na(.)))

Since "filter_at" is being depreciated in dplyr, how can I use "filter" and "across" to achieve a similar outcome?

We can use across to loop over the columns 'type', 'company' and return the rows that doesn't have any NA in the specified columns

library(dplyr)
df %>%
     filter(across(c(type, company), ~ !is.na(.)))
#     id  type company
#1  3 North    Alex
#2 NA North     BDA

With filter , there are two options that are similar to all_vars/any_vars used with filter_at/filter_all

df %>%
  filter(if_any(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

Or using if_all

df %>%
  filter(if_all(c(company, type), ~ !is.na(.)))
#   id  type company
#1  3 North    Alex
#2 NA North     BDA

data

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

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