Open In App

How to Select Rows with NA Values in R

Last Updated : 15 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 player3
1 4 NA NA
2 NA 3 9
3 11 3 4
4 15 6 NA
5 NA 8 7
6 20 14 10
7 2 NA 11
8 4 8 9

[1] "selecting the rows with NA values"
player1 player2 player3
1 4 NA NA
2 NA 3 9
4 15 6 NA
5 NA 8 7
7 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 vec2
1 NA 6
2 5 4
3 6 6
4 7 NA
5 NA NA
6 2 2
7 3 1
8 4 9

[1] "selecting the rows with NA values "
vec1 vec2
1 NA 6
4 7 NA
5 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 player3
1 4 NA NA
2 NA 3 9
3 11 3 4
4 15 6 NA
5 NA 8 7
6 20 14 10
7 2 NA 11
8 4 8 9

[1] "selecting a particular column containing NA values "
player1 player2 player3
2 NA 3 9
5 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  B
1 4 NA
2 NA 3
3 11 6
4 15 NA
5 NA 8
6 2 NA
7 4 8

[1] "selecting a particular column containing NA values "
A B
1 4 NA
4 15 NA
6 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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads