# 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:

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 the Statistics Globe Newsletter