How to Remove Rows with NA in One Specific Column in R


You can use one of the following three methods to remove rows with NA in one specific column of a data frame in R:

#use is.na() method
df[!is.na(df$col_name),]

#use subset() method
subset(df, !is.na(col_name))

#use tidyr method
library(tidyr)
df %>% drop_na(col_name)

Note that each of these methods will produce the same results.

The following examples show how to use each of these methods in practice with the following data frame:

#create data frame
df <- data.frame(a = c(NA, 14, 19, 22, 26),
                 b = c(14, NA, 9, NA, 5),
                 c = c(45, 56, 54, 57, 59))

#view data frame
df

   a  b  c
1 NA 14 45
2 14 NA 56
3 19  9 54
4 22 NA 57
5 26  5 59

Method 1: Remove Rows with NA Using is.na()

The following code shows how to remove rows from the data frame with NA values in a certain column using the is.na() method:

#remove rows from data frame with NA values in column 'b'
df[!is.na(df$b),]

   a  b  c
1 NA 14 45
3 19  9 54
5 26  5 59

Method 2: Remove Rows with NA Using subset()

The following code shows how to remove rows from the data frame with NA values in a certain column using the subset() method:

#remove rows from data frame with NA values in column 'b'
subset(df, !is.na(b))

   a  b  c
1 NA 14 45
3 19  9 54
5 26  5 59

Method 3: Remove Rows with NA Using drop_na()

The following code shows how to remove rows from the data frame with NA values in a certain column using the drop_na() method:

library(tidyr)

#remove rows from data frame with NA values in column 'b'
df %>% drop_na(b)

   a  b  c
1 NA 14 45
3 19  9 54
5 26  5 59

Notice that each of the three methods produced the same result.

Note: You can find the complete online documentation for the drop_na() method here.

Additional Resources

How to Apply Function to Each Row in Data Frame in R
How to Retrieve Row Numbers in R
How to Append Rows to a Data Frame in R

Leave a Reply

Your email address will not be published.