Removing NA observations with dplyr::filter()

emehex picture emehex · Mar 4, 2015 · Viewed 94.5k times · Source

My data looks like this:

library(tidyverse)

df <- tribble(
    ~a, ~b, ~c,
    1, 2, 3, 
    1, NA, 3, 
    NA, 2, 3
)

I can remove all NA observations with drop_na():

df %>% drop_na()

Or remove all NA observations in a single column (a for example):

df %>% drop_na(a)

Why can't I just use a regular != filter pipe?

df %>% filter(a != NA)

Why do we have to use a special function from tidyr to remove NAs?

Answer

JeffZheng picture JeffZheng · Aug 8, 2017

For example:

you can use:

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

to remove the NA in column a.