Table of Contents
In R, you can select rows with NA values by using the function is.na() to create a logical vector of TRUE and FALSE values with TRUE values corresponding to the NA values in the data frame. Then, you can use the logical vector to subset the data frame and select the rows with NA values.
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 <- data.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 <- df[!complete.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 <- df[is.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.
The following tutorials explain how to perform other common tasks in R: