Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in R Programming by (7.3k points)
edited by

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
edited by

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

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...