5 Ways to Remove Rows with NA in One Specific Column in R

Deepanshu Bhalla Add Comment ,

Below is a list of 5 different methods to remove rows with NA in one specific column of a data frame in R.

Method 1 : is.na() Function
newdf <- df[!is.na(df$column_name),]
Method 2 : subset() Function
newdf <- subset(df,!is.na(column_name))
Method 3 : complete.cases() Function
newdf <- df[complete.cases(df$column_name), ]
Method 4 : filter() Function
library(dplyr)
newdf <- filter(df,!is.na(column_name))
Method 5 : drop_na() Function
library(tidyr)
newdf <- df %>% drop_na(column_name)
Sample DataFrame

Let's prepare a sample dataframe for demonstration purposes. The name of dataframe is df. It contains two columns and 6 rows.

df <- data.frame(name = c('deeps','sandy', 'david', NA,'preet',NA),
                 sales = c(50, 100, 45, 100, 90, NA))
Remove Rows with NA in One Specific Column in R
Example 1 : Removing Rows with NA using is.na() Function

The code below shows how to remove rows from a data frame that have missing values (NA) in a particular column using the is.na() function.

newdf <- df[!is.na(df$name),]
Output
   name sales
1 deeps    50
2 sandy   100
3 david    45
5 preet    90

The code creates a new data frame called newdf by selecting rows from the original data frame "df" where the values in the column named name are not NA.

Example 2 : Removing Rows with NA using subset() Function

The following code demonstrates how to remove rows from a data frame if they contain missing values (NA) in a specific column using the subset() function.

newdf <- subset(df,!is.na(name))
Output
   name sales
1 deeps    50
2 sandy   100
3 david    45
5 preet    90

We selected rows from the original data frame "df" where the values in the "name" column are not missing (NA). The is.na() function is used to identify the rows with missing values, and the negation operator (!) is applied to select the rows where the values are not NA.

Example 3 : Removing Rows with NA using complete.cases() Function

The following code shows how to remove rows from a data frame with NA values in a particular column using the complete.cases() function.

newdf <- df[complete.cases(df$name), ]
Output
   name sales
1 deeps    50
2 sandy   100
3 david    45
5 preet    90
Example 4 : Removing Rows with NA using filter() Function

In the code below, we are using the filter function from the dplyr package and is.na() function to remove rows from a data frame with NA values in a specific column.

library(dplyr)
newdf <- filter(df,!is.na(name))
Output
   name sales
1 deeps    50
2 sandy   100
3 david    45
5 preet    90
Example 5 : Removing Rows with NA using drop_na() Function

Here we are using the drop_na function from the tidyr package to remove rows from a data frame with NA values in a specific column.

library(tidyr)
newdf <- df %>% drop_na(name)
Output
   name sales
1 deeps    50
2 sandy   100
3 david    45
5 preet    90
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 0 Response to "5 Ways to Remove Rows with NA in One Specific Column in R"
Next → ← Prev