How can I select rows with NA values in R?

How can I select rows with NA values in R?

In R, NA values represent missing or unknown data. To select rows with NA values, you can use the “is.na()” function in combination with the “which()” function. This will return a vector of logical values indicating which rows contain NA values. You can then use this vector to subset your data frame and select only the rows with NA values. Alternatively, you can use the “complete.cases()” function to exclude rows with NA values from your data frame. This will return a data frame with only complete cases, meaning rows without any NA values.

Select Rows with NA Values in R


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.

Cite this article

stats writer (2024). How can I select rows with NA values in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-select-rows-with-na-values-in-r/

stats writer. "How can I select rows with NA values in R?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-select-rows-with-na-values-in-r/.

stats writer. "How can I select rows with NA values in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-select-rows-with-na-values-in-r/.

stats writer (2024) 'How can I select rows with NA values in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-select-rows-with-na-values-in-r/.

[1] stats writer, "How can I select rows with NA values in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I select rows with NA values in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top