Laura
Laura

Reputation: 483

In dplyr filter rows with number of NA bigger than one

This is my code:

airquality %>% filter(across(everything(), ~ sum(is.na(.x)) > 1))

I am trying to filter the rows with more than one NAs.

The only way I achieve this was using mutate function.

airquality %>% rowwise() %>% mutate(n_NA = sum(is.na(across(everything())))) %>%
                  filter(n_NA > 1)

But its not a clever way.

I need to use only filter function.

Any help?

Upvotes: 2

Views: 548

Answers (1)

akrun
akrun

Reputation: 886948

We can use rowSums to create a logical expression within filter

library(dplyr)
airquality  %>% 
    filter(rowSums(is.na(.)) > 1)

-output

#   Ozone Solar.R Wind Temp Month Day
#1    NA      NA 14.3   56     5   5
#2    NA      NA  8.0   57     5  27

Or with rowwise and c_across

airquality %>%
   rowwise %>%
   filter(sum(is.na(c_across(everything()))) > 1)
# A tibble: 2 x 6
# Rowwise: 
#  Ozone Solar.R  Wind  Temp Month   Day
#  <int>   <int> <dbl> <int> <int> <int>
#1    NA      NA  14.3    56     5     5
#2    NA      NA   8      57     5    27

Upvotes: 3

Related Questions