NaN in R Explained (Example Code) | is.nan Function, Count, Replace & Remove

 

In the R programming language, NaN stands for Not a Number.

This article explains how to deal with NaN values in R. This includes the application of the is.nan R function.

Let’s dive in.

 

When does NaN Occur?

As shown in the following example, we can use R as regular calculator:

5 / 2                                        # Basic computation in R
# 2.5

However, if we try to run an invalid computation (e.g. 0 / 0), R returns NaN:

0 / 0                                        # Invalid computation returns NaN
# NaN

 

How to Find NaN in Data? [is.nan Function]

If we have a complex vector, data frame or matrix, it might be complicated to identify the NaN values in our data. In such a case, we can apply the is.nan function.

The is.nan function returns a logical vector or matrix, which indicates the NaN positions in our data.

Consider the following example vector:

x <- c(5, 9, NaN, 3, 8, NA, NaN)             # Create example vector in R

If we apply the is.nan function to this data, R returns a logical vector (i.e. TRUE or FALSE) to the console:

is.nan(x)                                    # Apply is.nan function
# FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE

In combination with the which R function, we can print the positions of our NaN values to the RStudio console:

which(is.nan(x))                             # Get positions of NaN
# 3 7

And in combination with the sum function, we can count the amount of NaN values in our data:

sum(is.nan(x))                               # Count amount of NaN
# 2

 

Remove NaN Values [!is.nan]

We can use the is.nan function in its reversed form by typing a bang in front of the function (i.e. !is.nan).

This can be used to exclude NaN values from our data:

x_remove <- x[!is.nan(x)]                    # Remove NaN from vector
x_remove                                     # Print reduced vector to RStudio
# 5  9  3  8 NA

You can read the previous code as follows: “R, please keep every element of our data that is not NaN

 

Replace NaN Values

Another alternative is the replacement of NaN values.

With the following R code, we replace NaN with 0:

x_replace <- x                               # Replicate example vector
x_replace[is.nan(x_replace)] <- 0            # Replace NaN with 0 in R
x_replace                                    # Print vector with replacement
# 5  9  0  3  8 NA  0

However, we could change the NaN values to basically every value we want.

 

What’s the Difference Between NaN & NA in R?

You might have noticed that R also uses the NA symbol to display data.

So what is the difference between NaN and NA? Why do we need two different symbols?!

Definition of NaN: NaN stands for Not a Number and is always displayed when an invalid computation was conducted.

Definition of NA: NA stands for Not Available and is used whenever a value is missing (e.g. due to survey nonresponse).

If you need some more details, you may also have a look at the definitions in the R documentation:

 

Difference of NaN and NA

Figure 1: R Documentations of NaN & NA.

 

Furthermore, you can learn more about NA values HERE and you can learn more about the is.na R function HERE.

 

Tutorial Video & Further Resources for the Handling of NaN in R

I have also published a video tutorial on this topic, so if you are still struggling with the code, watch the following video on my YouTube channel:

 

 

In case you want to learn more about NaN values in R, I can recommend the following YouTube video of Mr. Math Expert. He shows in the video how to compute the mean of data that contains NaN values.

 

 

In addition, you could have a look at some of the other R tutorials on my website:

This article showed how to apply deal with NaN values and the is.nan function in R. Leave me a comment below in case you have any feedback or questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top