Extract Subset of Data Frame Rows Containing NA in R (2 Examples)

 

In this article you’ll learn how to select rows from a data frame containing missing values in R.

The tutorial consists of two examples for the subsetting of data frame rows with NAs. To be more specific, the tutorial contains this information:

Here’s the step-by-step process…

 

Creation of Example Data

First, we’ll have to create some example data:

data <- data.frame(x1 = c(3, NA, 3, 5, NA),    # Create data
                   x2 = c(NA, 1, 2, 3, 4),
                   x3 = c(7, NA, 4, 1, 2))
data                                           # Print data
#   x1 x2 x3
# 1  3 NA  7
# 2 NA  1 NA
# 3  3  2  4
# 4  5  3  1
# 5 NA  4  2

As you can see based on the previous output of the RStudio console, our exemplifying data contains three columns. Each of the variables contains missing values.

 

Example 1: Extract Rows with NA in Any Column

In this Example, I’ll illustrate how to filter rows where at least one column contains a missing value. We are using a combination of the rowSums and the is.na functions for this task:

data[rowSums(is.na(data)) > 0, ]               # Missings in any row
#   x1 x2 x3
# 1  3 NA  7
# 2 NA  1 NA
# 5 NA  4  2

Three rows were selected from our data frame.

 

Example 2: Extract Rows with NA in Specific Column

This Section shows how to create a data frame subset where only one specific variable contains NA values.

data[is.na(data$x1), ]                         # Missings in x1
#   x1 x2 x3
# 2 NA  1 NA
# 5 NA  4  2

The previous RStudio console output shows a data frame where all rows with NA in the column x1 where kept.

 

Video, Further Resources & Summary

I have recently released a video on my YouTube channel, which explains the R programming syntax of the present article. You can find the video below:

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

In addition, you might have a look at some of the related tutorials of this website. You can find some articles about the selection of certain rows below:

 

In summary: At this point you should have learned how to filter data set rows with NA in R. In case you have additional comments or questions, don’t hesitate to let me know in the comments.

 

Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


2 Comments. Leave new

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top