0 votes
1 view
in R Programming by (5.1k points)

Is it possible to filter a data.frame for complete cases using dplyr? complete.cases with a list of all variables works, of course. But that is a) verbose when there are a lot of variables and b) impossible when the variable names are not known (e.g. in a function that processes any data.frame).

library(dplyr)

df = data.frame(

    x1 = c(1,2,3,NA),

    x2 = c(1,2,NA,5)

)

df %.%

  filter(complete.cases(x1,x2))

1 Answer

0 votes
by (24.7k points)

You can use the following for complete.cases in filter  function:

library(dplyr)

df = data.frame(

  x1 = c(1,2,3,NA),

  x2 = c(1,2,NA,5)

)

 df %>% filter(complete.cases(.))

  x1 x2

1  1  1

2  2  2

You can also use the tidyr package as follows:

library(tidyr)

df %>% drop_na

  x1 x2

1  1  1

2  2  2

If you want to filter based on one variable's missingness, use a conditional as follows:

df %>% filter(!is.na(x1))

  x1 x2

1  1  1

2  2  2

3  3 NA

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...