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?
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).
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.
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.
For example:
you can use:
df %>% filter(!is.na(a))
to remove the NA in column a.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With