How to remove row if it has a NA value in one certain column

r na
user9259005 picture user9259005 · Feb 7, 2018 · Viewed 75.4k times · Source

My data called "dat":

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

I want to be all rows to be removed if it has an NA in column B:

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

na.omit(dat) removes all rows with an NA not just the ones where the NA is in column B.

Also I'd like to know how to this for NA value in two columns.

I appreciate all advice!

Answer

clemens picture clemens · Feb 7, 2018

The easiest solution is to use is.na():

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

which gives you:

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