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 to the requirement. To count the total NA values present in the data we have to use the sum() function
Syntax:
sum(is.na(data))
To get the positions where NA values are there, by using which() function
Syntax:
which(is.na(data))
Use of is.na in vector
A vector is a data structure that can store elements of multiple data types.
Example: R program to get and count NA values in a vector
R
# create a vector
data = c(1, 2, 3, NA, 45, 34, NA, NA, 23)
# display
print(data)
# get NA values
print(is.na(data))
# count NA values
print(sum(is.na(data)))
# get the NA index positions
print(which(is.na(data)))
Output:
[1] 1 2 3 NA 45 34 NA NA 23
[1] FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE FALSE
[1] 3
[1] 4 7 8
Use of is.na in dataframe
A dataframe is a data structure that can stores elements of multiple data type in rows and columns
Example: R program to count NA and get NA values in a dataframe
R
# create a dataframe with 3 columns
data=data.frame(column1=c(1,2,NA,34),
column2=c(NA,34,56,NA),
column3=c(NA,NA,32,56))
# display
print(data)
# get NA values
print(is.na(data))
# count NA values
print(sum(is.na(data)))
# get the NA index positions
print(which(is.na(data)))
Output:
We can use sapply() function to get total NA values in the dataframe.
Syntax:
sapply(dataframe, function(variable) sum(is.na(variable)))
where
- dataframe is the input dataframe
- function is to get sum of NA in each column
Example: Use of is.na on a dataframe
R
# create a dataframe with 3 columns
data=data.frame(column1=c(1,2,NA,34),
column2=c(NA,34,56,NA),
column3=c(NA,NA,32,56))
# display
print(data)
# get count of NA in each column
print(sapply(data, function(x) sum(is.na(x))))
Output:
Similar Reads
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 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 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 Use the (?) Operator in R The ? operator in R is a simple yet powerful tool that provides quick access to documentation and help pages for functions, datasets, and other objects within the R environment. Understanding how to effectively use this operator can significantly enhance your productivity and help you learn R Progra
4 min read
How to Include NA in ifelse() Using R? The if-else () function in R is a powerful and efficient way to apply conditional logic across vectors, matrices, or data frames. However, dealing with NA (missing values) in if-else () can be tricky. This article will explore how to handle and include NA values effectively when using if-else () in
3 min read