R is.na() Function

The is.na() function in R is used to check for missing values (NA) in the data frame, dataset, or vector.

This function is extremely helpful in data cleaning and preparation, as it helps identify and handle missing values in a dataset.

Syntax

is.na(df)

Parameters

df: It is a data frame or set to be tested.

Return value

It returns a boolean or logical value: TRUE if it finds the NA value and FALSE if it does not.

Example 1: Using is.na() function with DataFrame

If you have a data frame and you are not sure how many NA values are there in the data frame, you can use the is.na() function and pass the data frame will return a data frame where NA values are replaced by TRUE and, in another case, FALSE.

Figure of using is.na() function with DataFrame

df <- data.frame(
  col1 = c(1, NA, 3),
  col2 = c(NA, 5, NA),
  col3 = c(7, NA, 9)
)

is.na(df)

Output

Output of is.na() function with data frame

Example 2: Working with Vector

Figure of using is.na() function with Vector

From the visual representation, you can see that we created a vector with two NA values and use is.na() function that will return TRUE for NA values and FALSE otherwise.

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

is.na(vec)

Output

[1] FALSE  FALSE  FALSE  TRUE  FALSE  TRUE

Example 3: Working with any() function

The any() function returns whether any 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 have a single NA value, then it returns FALSE.

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

any(is.na(data))

Output

[1] FALSE

Example 4: Counting NA values in a data frame

When you are doing exploratory data analysis, finding and removing NA values is the most important part and these functions will help you find it.

If you want to count total NA values in a data frame, use the combination of is.na() and sum() functions.

Let’s take an example data frame df and count the NA values.

Counting the number of NA values in R data frame

df <- data.frame(
  col1 = c(1, NA, 3),
  col2 = c(NA, 5, NA),
  col3 = c(7, NA, 9)
)

num_na_df <- sum(is.na(df))

num_na_df

Output

[1] 4

Example 5: Counting NA values in a vector

You can count the number of NA values in a vector using the combination of sum() and is.na() functions.

Figure of counting NA values in a vector

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

sum(is.na(vec))

Output

[1] 2

To deal with NA values, you might use functions like na.omit() to remove rows with NA or functions like replace(), mean(), median(), etc., to impute missing values.

Leave a Comment