R Basic

is.nan() Function: Handling NaN Values in R

The is.nan() is a built-in R function that checks if a vector or any valid object contains “Not a Number (NaN)” values. It returns a logical vector of the same length as the input, with each element being TRUE if the corresponding element in the input is NaN and FALSE otherwise.

vec <- c(11, NaN, 21, 19, NaN)

print(vec) # [1] 11 NaN 21 19 NaN

is.nan(vec) # [1] FALSE TRUE FALSE FALSE TRUE

You can see in the above code that is.nan() function returns TRUE for NaN values and FALSE otherwise.

NaN is a special numeric value that results from undefined numerical operations like division by 0 or the square root of a negative value.

By default, this function works with numeric values.

Why should we handle NaN values?

Any arithmetic operation involving NaN values results in NaN output because the precise nature of NaN is unknown, which can lead to unexpected errors or incorrect results.

NaN is different from NA and NULL. All these values have a specific purpose.

With the help of this function, we can identify the source of undefined operations that output NaN while debugging, which helps us prevent unexpected errors.

With this function, you can identify NaN values in your dataset and either remove them or replace them with the mean of that dataset, making your output accurate or near-accurate.

Syntax

is.nan(obj)

Parameters

Name Value
obj It is an input R object that should be a numeric vector by default.

Performing 0/0

If you perform 0 / 0 or any value divided by 0, it will return the NaN value, which you can verify using this function.

zero_div <- 0 / 0

print(vec) # [1] NaN

is.nan(vec) # [1] TRUE

Handling NaN values

Let’s replace NaN values with 0 in the vector (vec) and see the output.

# Replace NaN with 0

vec[is.nan(vec)] <- 0

print(vec) # [1] 11 0 21 19 0

We only assigned 0 to those whose values are NaN.

Lists

The list is a collection of different data types, and when you apply a list as an argument to it.nan() function will give you an error because it cannot handle the list directly.

You must check for each element of the list using the lapply() function, and if that element is NaN, it returns TRUE for that element and FALSE otherwise.

main_list <- list(1, NaN, "KB", c(21, NaN, NaN))

nan_check <- lapply(main_list, \(x) if (is.numeric(x)) is.nan(x) else FALSE)

nan_check

Output

[[1]]
[1] FALSE

[[2]]
[1] TRUE

[[3]]
[1] FALSE

[[4]]
[1] FALSE  TRUE  TRUE

As you can see:

  1. The first element of list 19 is not NaN, so it returns FALSE.
  2. The second element is NaN, so it returns TRUE.
  3. The third element is “KB”, which is not even a numeric value, so it returns FALSE.
  4. The fourth element is a numeric vector containing other numeric values. So, it will check for each element, and if NaN is found, it returns TRUE; otherwise, it returns FALSE. If the last vector contained character values, it would have returned FALSE.

Matrix

This function checks element-wise in the matrix, like a vector, and returns TRUE for NaN and FALSE otherwise.

main_mtrx <- matrix(c(11, 19, NaN, 21), nrow = 2)

main_mtrx 

is.nan(main_mtrx)

Output

Data Frame

Data Frame is a complex data type you cannot use directly.nan() function with it. What you can do is pass an individual column of a data frame, and then it will act as a vector, and this function will check if it contains NaN.

df <- data.frame(a = c(1, NaN, 3), b = c(4, 5, NaN), c = c("a", "b", NaN))

# is.nan(df)
# Output: Error in is.nan(df) : default method not implemented for type 'list'

# Correct way: apply to columns
is.nan(df$a)
# Output: [1] FALSE TRUE FALSE

is.nan(df$b)
# Output: [1] FALSE FALSE TRUE

is.nan(df$c)
# Output: [1] FALSE FALSE FALSE

In the above code, we directly applied our function to df, which resulted in an error. The code is already commented.

Then, we applied a function to individual columns, and that’s where it started working correctly; we obtained the output vector of the same size as the column’s size.

That’s all, mates!

Recent Posts

colSums(): Calculating the Sum of Columns of a Data Frame in R

The colSums() function in R calculates the sums of columns for numeric matrices, data frames,…

5 days ago

rowSums(): Calculating the Sum of Rows of a Matrix or Data Frame in R

The rowSums() function calculates the sum of values in each numeric row of a matrix,…

1 week ago

R View() Function

The View() is a utility function in R that invokes a more intuitive spreadsheet-style data…

2 weeks ago

summary() Function: Producing Summary Statistics in R

The summary() is a generic function that produces the summary statistics for various R objects,…

3 weeks ago

R paste() Function

The paste() function in R concatenates vectors after converting them to character. paste("Hello", 19, 21,…

4 weeks ago

paste0() Function in R

R paste0() function concatenates strings without any separator between them. It is a shorthand version…

4 weeks ago