# How to Select Rows with NA Values in R

Last Updated : 15 Mar, 2024

In this article, we will examine various methods to select rows with NA values in the R programming language.

## What are NA values?

NA represents ‘not available’ used for indicating the missing values or undefined data in the datasets. It is a logical constant of length 1. NA is one of the reserved words whose properties differ from others. It is one of the common issues while dealing with large amounts of data sets. Dealing with NA values is a crucial step in data analysis.

## How do we select rows containing NA values?

R provides many in-built or predefined functions to deal with NA values. This NA can be able to presented in different data sets such as data frames, matrices, and vectors. R provides some functions or methods to select rows containing NA values. Some of the methods are:

1. complete.cases()
2. is.na()

### Selecting the rows using complete.cases()

The function complete.cases() is one of the in-built function, that can have the ability to select or remove the NA values in the data sets, such as data frames, vectors and matrices. Using the function complete.cases(), can work with NA values efficiently.

### Syntax:

`complete.cases( x )x : parameter`

Here, created a data frame. Using the function complete.cases(), for selecting rows containing NA values. In the below example the function ‘df[!complete.cases(df),]’ represents selecting the rows containing at least one NA.

R ```#declaring a dataframe df <- data.frame(player1=c(4, NA, 11, 15, NA, 20, 2,4), player2=c(NA, 3, 3, 6, 8, 14, NA,8), player3=c(NA, 9, 4, NA, 7, 10, 11,9)) print(df) res <- df[!complete.cases(df),] print("selecting the rows with NA values") print(res) ```

Output:

`  player1 player2 player31       4      NA      NA2      NA       3       93      11       3       44      15       6      NA5      NA       8       76      20      14      107       2      NA      118       4       8       9[1] "selecting the rows with NA values"  player1 player2 player31       4      NA      NA2      NA       3       94      15       6      NA5      NA       8       77       2      NA      11`

Here we created a data frame, by using different vectors. Using the function complete.cases(), for selecting rows containing NA values. Here, the function ‘df[!complete.cases(df),]’ represents selecting the rows containing at least one NA.

R ```#declaring a dataframe vec1=c(NA,5,6,7,NA,2,3,4) vec2=c(6,4,6,NA,NA,2,1,9) df <- data.frame(vec1,vec2) print(df) res <- df[!complete.cases(df),] print("selecting the rows with NA values ") print(res) ```

Output:

`  vec1 vec21   NA    62    5    43    6    64    7   NA5   NA   NA6    2    27    3    18    4    9[1] "selecting the rows with NA values "  vec1 vec21   NA    64    7   NA5   NA   NA`

### Selecting rows with NA Values in Any Column

The function is.na() is used to identify NA values. It is a logical vector and can have the ability to select or remove NA values in the data sets. Selecting and accessing rows with NA values is a crucial technique while dealing with missing data(NA). Using the function is.na(), we can efficiently work with NA values.

### Syntax:

`is.na(x)x : parameter`

Here created a data frame. Using the function is.na(), we get the rows with NA values. The function ‘df[is.na(df\$player1), ]’ indicates selecting the rows based on the column name(player1) containing NA values.

R ```#declaring a dataframe df <- data.frame(player1=c(4, NA, 11, 15, NA, 20, 2,4), player2=c(NA, 3, 3, 6, 8, 14, NA,8), player3=c(NA, 9, 4, NA, 7, 10, 11,9)) print(df) res <- df[is.na(df\$player1), ] print("selecting a particular column containing NA values ") print(res) ```

Output:

`  player1 player2 player31       4      NA      NA2      NA       3       93      11       3       44      15       6      NA5      NA       8       76      20      14      107       2      NA      118       4       8       9[1] "selecting a particular column containing NA values "  player1 player2 player32      NA       3       95      NA       8       7`

Here created a data frame. Using the function is.na(), we get the rows with NA values. The function ‘df[is.na(df\$B),]’ indicates selecting the rows based on the column name(B) containing NA values.

R ```#declaring a dataframe df <- data.frame(A=c(4, NA, 11, 15, NA, 2,4), B=c(NA, 3, 6, NA,8, NA, 8) ) print(df) res <- df[is.na(df\$B), ] print("selecting a particular column containing NA values ") print(res) ```

Output:

`   A  B1  4 NA2 NA  33 11  64 15 NA5 NA  86  2 NA7  4  8[1] "selecting a particular column containing NA values "   A  B1  4 NA4 15 NA6  2 NA`

## Conclusion

In this article, we accomplished two methods to select rows with NA values by using the functions complete.cases() and is.na(). Here, R provided many tools for dealing with these NA values.