Home » How to Select Rows with NA Values in R

How to Select Rows with NA Values in R

by Tutor Aspire

You can use the following methods to select rows with NA values in R:

Method 1: Select Rows with NA Values in Any Column

df[!complete.cases(df), ]

Method 2: Select Rows with NA Values in Specific Column

df[is.na(df$my_column), ]

The following examples show how to use each method with the following data frame in R:

#create data frame
df frame(points=c(4, NA, 10, 14, 15, NA, 20, 22),
                 rebounds=c(NA, 3, 3, 7, 6, 8, 14, 10),
                 assists=c(NA, 9, 4, 4, 3, 7, 10, 11))

#view data frame
df

  points rebounds assists
1      4       NA      NA
2     NA        3       9
3     10        3       4
4     14        7       4
5     15        6       3
6     NA        8       7
7     20       14      10
8     22       10      11

Example 1: Select Rows with NA Values in Any Column

The following code shows how to select rows with NA values in any column of the data frame in R:

#select rows with NA values in any column
na_rows cases(df), ]

#view results
na_rows

  points rebounds assists
1      4       NA      NA
2     NA        3       9
6     NA        8       7

Notice that the rows with NA values in any column are selected.

Example 2: Select Rows with NA Values in Specific Column

The following code shows how to select rows with NA values in a specific column of the data frame in R:

#select rows with NA values in the points column
na_rows na(df$points), ]

#view results
na_rows

  points rebounds assists
2     NA        3       9
6     NA        8       7

Notice that only the rows with NA values in the points column are selected.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Use complete.cases in R
How to Use na.omit in R
How to Remove Empty Rows from Data Frame in R

You may also like