is.na in r: How to Use is.na() function in R

When working in a real-time dataset, you won’t get clean data. Real-world data is always filled with missing values. So if you are developing an algorithm that can predict the future values, you first need to clean the existing data, and based on that, you can create a model and feed the data.

What is missing values in R

Missing values in R are represented by the NA (not available) values. Most modeling functions in R offer options for dealing with missing values. You can go further with the pairwise or listwise deletion of missing values through methods such as multiple imputations. 

R datasets are mostly filled with empty or NaN or NA values which you need to clean.  The na.omit() in the R function removes the rows of data containing the NA value.

You can also use the is.na() function to check if the particular R object contains any NA value or not. Let’s discuss the is.na() method in detail.

is.na in r

To check if the value is NA in R, use the is.na() function. The is.na() is a built-in R function that returns TRUE if it finds NA value and FALSE if it does not find in the dataset. If the value is NA, the is.na() function returns TRUE, otherwise, returns FALSE.

Syntax

is.na(x)

Arguments

The is.na() function takes an R Object to be tested.

Return value

The default method for is.na() applied to an atomic vector returns a logical vector of the same length as its argument x, containing TRUE for those elements marked NA or, for numeric or complex vectors, NaN, and FALSE otherwise.

Example

data <- c(11, 21, 19, NA, 46, NA)
is.na(data)

Output

[1] FALSE  FALSE  FALSE  TRUE  FALSE  TRUE

You can see that if the is.na() function encounters the NA values, it returns TRUE otherwise FALSE.

Using any() function with is.na() function

The any() is a built-in R function that returns whether any of the values are NA in the input object.

data <- c(11, 21, 19, NA, 46, NA)

any(is.na(data))

Output

[1] TRUE

In this example, any() function returns TRUE because the vector data contains at least one NA value. If it does not contain a single NA value then it returns FALSE.

data <- c(11, 21, 19, 46, 18)

any(is.na(data))

Output

[1] FALSE

When combined with the any() function with is.na(), it becomes a very powerful tool for dealing with missing data in a data set. 

That’s it for is.na() function in R.

Leave a Comment