Patrick
Patrick

Reputation: 1227

Using dplyr to select rows containing non-missing values in several specified columns

Here is my data

data <- data.frame(a= c(1, NA, 3), b = c(2, 4, NA), c=c(NA, 1, 2))

I wish to select only the rows with no missing data in colunm a AND b. For my example, only the first row will be selected.

I could use

data %>% filter(!is.na(a) & !is.na(b))

to achieve my purpose. But I wish to do it using if_any/if_all, if possible. I tried data %>% filter(if_all(c(a, b), !is.na)) but this returns an error. My question is how to do it in dplyr through if_any/if_all.

Upvotes: 1

Views: 1908

Answers (3)

jpsmith
jpsmith

Reputation: 17174

This could do the trick - use filter_at and all_vars in dplyr:

data %>% 
   filter_at(vars(a, b), all_vars(!is.na(.)))

Output:

#  a b  c
#1 1 2 NA

Upvotes: 1

Jilber Urbina
Jilber Urbina

Reputation: 61154

data %>% 
  filter(if_all(c(a,b), ~!is.na(.)))

  a b  c
1 1 2 NA

Upvotes: 2

akrun
akrun

Reputation: 886938

We could use filter with if_all

library(dplyr)
data %>% 
   filter(if_all(c(a,b), complete.cases))

-output

  a b  c
1 1 2 NA

Upvotes: 1

Related Questions