This tutorial shows how to remove rows with NA in one column in R. There are two R syntax to remove rows with NA in a single one column.
# Use is.na() function dataframe_name[!is.na(dataframe_name$column_name),] # Use na.omit() function na.omit(dataframe_name, cols = "column_name")
Sample Data Frame Being Used
The following R code to generate a sample dataframe before removing any rows with NA.
# create a data frame df <- data.frame (cities = rep(c('City1','City2'),each=5), stores = rep(c('store1','store2','store3','store4',NA), 2), sales=c(10,NA,20,50,30,10,5,4,12,4)) # print out the data frame print(df)
The following is the sample dataframe. There are 10 rows of data.
> print(df)
cities stores sales
1 City1 store1 10
2 City1 store2 NA
3 City1 store3 20
4 City1 store4 50
5 City1 <NA> 30
6 City2 store1 10
7 City2 store2 5
8 City2 store3 4
9 City2 store4 12
10 City2 <NA> 4
Examples of Remove Rows with NA in One column in R
Example 1: Use is.na()
In the following, we are going to use is.na()
to remove rows with NA
in the column of store
s.
# remove rows with NA in the column of "stores"
df[!is.na(df$stores),]
The following is the output. As we can see, there is no NA
rows in the column of stores
anymore.
> df[!is.na(df$stores),]
cities stores sales
1 City1 store1 10
3 City1 store3 20
4 City1 store4 50
6 City2 store1 10
7 City2 store2 5
8 City2 store3 4
9 City2 store4 12
Example 2: Use na.omit()
# remove rows with NA in the column of "stores"
na.omit(df, cols = "stores")
The following is the output. As we can see, there is no NA
rows in the column of stores
anymore.
> na.omit(df, cols = "stores")
cities stores sales
1 City1 store1 10
3 City1 store3 20
4 City1 store4 50
6 City2 store1 10
7 City2 store2 5
8 City2 store3 4
9 City2 store4 12