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
Example 1: Extract Rows with NA in Any Column
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:
- colSums, rowSums, colMeans & rowMeans in R
- Subset Data Frame Rows by Logical Condition
- Conditionally Remove Row from Data Frame
- Extract Row from Data Frame
- Extract First N Rows of Data Frame in R
- The R Programming Language
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.