Home > Software design >  Using dplyr to select rows containing non-missing values in several specified columns
Using dplyr to select rows containing non-missing values in several specified columns

Time:10-26

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.

CodePudding user response:

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

  a b  c
1 1 2 NA

CodePudding user response:

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

CodePudding user response:

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
  • Related