Open In App

What is the difference between NA and NAN in R?

Last Updated : 12 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

R Programming Language is a super popular programming language for analyzing data. Lots of data scientists, statisticians, and researchers love using it because it’s so versatile and has lots of tools to help them out. But sometimes, figuring out all the little details can’t be easy. One thing that often confuses people is understanding the difference between NA and NaN. They might look similar, but they’re actually used for different things in R.

What is NA?

NA stands for “Not Available.” It’s like a placeholder for when data is missing or doesn’t exist. For example, suppose we’re creating a list of students’ ages, and one student hasn’t provided their age. In the dataset, that missing age would be marked as NA.

R
# Create a vector with NA values
data <- c(10, 20, NA, 30, NA, 40)

# Check which elements are NA
print("Indices of NA values:")
na_indices <- is.na(data)
print(na_indices) 

# Replace NA values with 0
print("Vector with NA values replaced by 0:")
data[na_indices] <- 0
print(data) 

# Calculate the mean of the vector, ignoring NA values
print("Mean value of the vector (excluding NA values):")
mean_value <- mean(data, na.rm = TRUE)
print(mean_value)  

Output:

[1] "Indices of NA values:"
[1] FALSE FALSE TRUE FALSE TRUE FALSE

[1] "Vector with NA values replaced by 0:"
[1] 10 20 0 30 0 40

[1] "Mean value of the vector (excluding NA values):"
[1] 16.66667

What is NaN ?

NaN stands for “Not a Number.” It’s used to show when a math operation doesn’t give a meaningful result. For example, if we try to divide zero by zero or take the square root of a negative number, we’ll get NaN.

R
# Perform a division by zero operation to generate NaN
result <- 0 / 0

# Check if the result is NaN
print("Is the result NaN?")
is_nan <- is.nan(result)
print(is_nan)
# Attempt to calculate the square root of a negative number to generate NaN
result <- sqrt(-1)

# Check if the result is NaN
print("Is the result NaN?")
is_nan <- is.nan(result)
print(is_nan) 

Output:

[1] "Is the result NaN?"
[1] TRUE
Warning message:
In sqrt(-1) : NaNs produced
[1] "Is the result NaN?"
[1] TRUE

Difference between NA and NAN

Aspect

NA

NaN

Definition

“Not Available”

“Not a Number”

Data Type

Any

Numeric

Meaning

Indicates missing data

Indicates an undefined result

Usage

Commonly used for missing or unavailable data

Commonly used for undefined numerical operations

Behavior in operations

Spreadsthrough computations involving NA values

Spreads and contaminates other calculations

Example

x <- c(1, 2, NA, 4)

0/0

Conclusion

NA and NaN serve different purposes in R. NA, which stands for “Not Available,” is used to represent missing or undefined data in datasets. On the other hand, NaN, short for “Not a Number,” is used to indicate undefined or unrepresentable numerical values resulting from specific mathematical calculations. Understanding the distinction between NA and NaN is essential for accurately handling missing data and ensuring the integrity of mathematical operations in R.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads