How to Handle NaN Values in R

Spread the love

Working with data is a complex task, particularly when the data isn’t clean. One of the most common issues you may encounter is missing or undefined values. In the R programming language, one type of missing value is represented by NaN, which stands for “Not a Number”. This article aims to provide an exhaustive guide on how to handle NaN values in R effectively, ensuring your analyses or data manipulations are not compromised.

What is NaN?

Before we get into the practical aspect of handling NaN values, it’s important to understand what they are. In R, NaN (Not a Number) is a special type of value that is undefined or unrepresentable. Generally, NaN values arise from undefined mathematical operations. For example:

0 / 0  # Produces NaN
sqrt(-1)  # Produces NaN

The NaN value is a member of the numeric data type, and it is considered to be different from NA (Missing Value) and Inf (Infinity).

Identifying NaN Values

Before you can deal with NaN values, you need to identify them in your dataset. You can identify NaN values using the is.nan() function.

vec <- c(1, 2, NaN, 4, 5)
is.nan(vec)  # Returns FALSE FALSE  TRUE FALSE FALSE

Removing NaN Values

Using na.omit( )

The na.omit() function removes NA and NaN values from an object.

vec <- c(1, 2, NaN, 4, 5)
clean_vec <- na.omit(vec)

Using Logical Indexing

You can also use logical indexing to remove NaN values.

vec <- c(1, 2, NaN, 4, 5)
clean_vec <- vec[!is.nan(vec)]

Replacing NaN Values

Using replace( )

The replace() function can be used to replace NaN values with a specific value.

vec <- c(1, 2, NaN, 4, 5)
vec <- replace(vec, is.nan(vec), 0)  # Replace NaN with 0

Using ifelse( )

You can also use the ifelse() function to replace NaN values conditionally.

vec <- c(1, 2, NaN, 4, 5)
vec <- ifelse(is.nan(vec), 0, vec)  # Replace NaN with 0

Aggregation and NaN

Functions like mean(), sum(), and min() do not consider NaN values.

vec <- c(1, 2, NaN, 4, 5)
mean(vec, na.rm = TRUE)  # Calculates mean after removing NaN


Imputing NaN values means replacing them with statistical estimates rather than simply removing them.

Mean Imputation

Replace NaN with the mean of the column.

vec <- c(1, 2, NaN, 4, 5)
mean_val <- mean(vec, na.rm = TRUE)
vec[is.nan(vec)] <- mean_val

Median Imputation

Replace NaN with the median of the column.

vec <- c(1, 2, NaN, 4, 5)
median_val <- median(vec, na.rm = TRUE)
vec[is.nan(vec)] <- median_val

Data Transformation

Standardizing Data

NaN values can disrupt data standardization, so handle them before scaling features.

vec <- c(1, 2, NaN, 4, 5)
mean_val <- mean(vec, na.rm = TRUE)
std_dev <- sd(vec, na.rm = TRUE)
vec[!is.nan(vec)] <- (vec[!is.nan(vec)] - mean_val) / std_dev

Data Binning

cut() function will return NaN for bins that have NaN values.


Handling NaN values in R involves understanding the nature of the dataset, the cause of the NaN values, and the best method for either removing or replacing them. With the techniques presented here, you’ll be well-equipped to handle NaN values effectively in your R data projects.

Posted in RTagged

Leave a Reply