0

Given a small dataset as follows:

A   B   C
1   2   NA
NA  2   3
1   NA  3
1   2   3

How could I remove rows based on the condition: columns B and C have NAs?

The expected result will like this:

A   B   C
NA  2   3
1   2   3
ah bon
  • 5,121
  • 5
  • 26
  • 65

2 Answers2

2

With base R:

df[!is.na(df$B) & !is.na(df$C),]

Using dplyr:

df %>%
  filter(!is.na(B), !is.na(C))

returns

# A tibble: 2 x 3
      A     B     C
  <dbl> <dbl> <dbl>
1    NA     2     3
2     1     2     3

or

df %>%
  drop_na(B, C)
Martin Gal
  • 4,180
  • 3
  • 10
  • 23
2

Another option in Base R is

df[complete.cases(df[c("B","C")]),]

   A B C
2 NA 2 3
4  1 2 3
Daniel O
  • 4,143
  • 4
  • 20