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:
- complete.cases()
- 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.
Similar Reads
How to Use na.rm in R?
In this article, we will discuss how to use na.rm in R Programming Language. na.rm in R is used to remove the NA values. na.rm in vector When we perform any operation, we have to exclude NA values, otherwise, the result would be NA. Syntax: function(vector,na.rm) where vector is input vectorna.rm is
3 min read
How to remove NA values with dplyr filter
In this article, we will examine various methods to remove NA values with dplyr filter by using R Programming Language. Remove NA values with the dplyr filterR language offers various methods to remove NA values with dplyr filter efficiently. By using these methods provided by R, it is possible to r
3 min read
Remove rows with missing values using R
Missing values are the data points that are absent for a specific variable in a dataset. It can be represented in various ways such as Blank spaces, null values or any special symbols like"NA". Because of these various reasons missing values can occur, such as data entry errors, malfunction in equip
3 min read
How to Use aggregate and Not Drop Rows with NA in R
In R Programming Language the aggregate() function is used to compute summary statistics by group. By default, aggregate() drop any rows with missing values (NA) in the grouping columns. However, we can specify the argument na.action = na.pass to retain rows with NA values during aggregation. Let us
3 min read
How to Use na.omit in R?
What are missing values?In data analysis, missing values refer to the absence of data for a particular variable or observation. These missing values are typically represented by a special symbol or code, often denoted as "NA" (Not Available) in R and many other programming languages. na.omit() funct
2 min read
How to Replace NA with Zero in dplyr
Missing values, denoted as NA, are a common occurrence in datasets and can pose challenges during data analysis and visualization. Handling missing values appropriately is crucial for accurate analysis and interpretation of data. In R Programming Language the dplyr package offers efficient tools for
3 min read
How to Use is.na in R?
In this article we will discuss how to use is.na in R programming language. is.na is used to check NA values present in the given data and return TRUE if the value is NA, otherwise FALSE Syntax: is.na(data) where, data is a vector/dataframe is.na() can be used with other methods to add more meaning
2 min read
How to Remove rows based on count of a specific value in R?
Data cleaning is an essential step in data analysis, and removing rows based on specific criteria is a common task. One such criterion is the count of a specific value in a column. This article will guide you through the process of removing rows from a data frame in R based on the count of a specifi
5 min read
How to Use "Is Not NA" in R?
In this article, we will discuss how to use Is Not NA in R Programming Language. NA is a value that is not a number. The is.na() method is used to check whether the given value is NA or not, we have to use the function for this. Inorder to use is NOT Â NA, then we have to add the "!" operator to the
2 min read
How to find missing values in a matrix in R
In this article, we will examine various methods for finding missing values in a matrix by using R Programming Language. What are missing values?The data points in a dataset that are missing for a particular variable are known as missing values. These missing values are represented in various ways s
3 min read