Extracting Rows with Missing Values in R

This article illustrates how to filter data set rows with NA in the R programming language.

Constructing Example Data

my_df <- data.frame(x = c(1:5, NA),    # Our data frame
                   y = c(NA, 1:5),
                   z = c(NA, NA, 2:5))
my_df
#    x  y  z
# 1  1 NA NA
# 2  2  1 NA
# 3  3  2  2
# 4  4  3  3
# 5  5  4  4
# 6 NA  5  5

Example 1: Returning Rows with Missing Values in Any Variable of the Data Frame

my_df[rowSums(is.na(my_df)) > 0, ]     # All NA-rows
#    x  y  z
# 1  1 NA NA
# 2  2  1 NA
# 6 NA  5  5

Example 2: Selecting Rows with Missing Values in a Certain Variable of the Data Frame

my_df[is.na(my_df$x), ]                # NA in variable x
#    x y z
# 6 NA 5 5

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top