Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing NA observations with dplyr::filter()

Tags:

r

dplyr

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?

like image 601
emehex Avatar asked Mar 04 '15 14:03

emehex


People also ask

How do I remove Na observations in R?

To remove all rows having NA, we can use na. omit function. For Example, if we have a data frame called df that contains some NA values then we can remove all rows that contains at least one NA by using the command na. omit(df).

How do I remove cells with NA in R?

The na. omit() function returns a list without any rows that contain na values. It will drop rows with na value / nan values. This is the fastest way to remove na rows in the R programming language.

How do I exclude data with NA in R?

First, if we want to exclude missing values from mathematical operations use the na. rm = TRUE argument. If you do not exclude these values most functions will return an NA . We may also desire to subset our data to obtain complete observations, those observations (rows) in our data that contain no missing data.


1 Answers

For example:

you can use:

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

to remove the NA in column a.

like image 104
JeffZheng Avatar answered Sep 23 '22 18:09

JeffZheng