01 / 21
NA NaN Inf

NA, NaN & Inf
in R Programming

Master how R handles missing values, undefined math, and infinity: the three special values every R programmer must understand.

R Programming

What Are Special Values in R?

Every programming language needs ways to represent data that isn't a normal number. R has three special values you'll encounter constantly when working with real-world data:

NA
Not Available: data that is missing or unknown
NaN
Not a Number: result of undefined math operations
Inf
Infinity: result of dividing a nonzero number by zero

Why Do They Matter?

Real datasets are messy. Surveys have unanswered questions, sensors fail, and formulas sometimes blow up. If you ignore special values, your entire analysis can silently produce wrong results.

1
Data integrity: one NA in a vector makes mean() return NA by default
2
Silent propagation: \( \text{NA} + 5 = \text{NA} \), not 5
3
Logical traps: \( \text{NA} == \text{NA} \) returns NA, not TRUE

Where Do They Come From?

NA Missing survey answer Sensor failure NaN
\( 0/0 \)
\( \text{Inf} - \text{Inf} \)
Inf
\( 1/0 \)
\( \exp(999) \)
x <- c(1, NA, 3) mean(x) # NA
\( 0/0 \)
# NaN
\( \sqrt{-1} \)
\( 1/0 \)
# Inf
\( -1/0 \) # -Inf
Propagates as NA Propagates as NaN Obeys math rules

The Propagation Rule

The most important rule in R: most arithmetic and logical operations involving NA return NA. This is called "propagation" because the missing-ness spreads through your calculations.

# NA propagation in action 5 + NA [1] NA NA > 3 [1] NA NA == NA [1] NA

Understanding NaN

NaN (Not a Number) arises from mathematically undefined operations. In R, NaN is technically a special type of NA: every NaN is an NA, but not every NA is a NaN.

# Operations that produce NaN 0 / 0 [1] NaN sqrt(-1) [1] NaN log(-1) [1] NaN Inf - Inf [1] NaN Inf / Inf [1] NaN # Testing for NaN is.nan(NaN) [1] TRUE is.na(NaN) [1] TRUE is.nan(NA) [1] FALSE

Understanding Inf and -Inf

R represents positive and negative infinity as Inf and -Inf. Unlike NA, Inf follows mathematical rules and does not automatically propagate missing-ness.

# Creating Inf 1 / 0 [1] Inf -1 / 0 [1] -Inf # Inf follows math rules Inf + 100 [1] Inf Inf * -1 [1] -Inf 1 / Inf [1] 0 Inf > 999999 [1] TRUE # But some Inf operations produce NaN Inf - Inf [1] NaN Inf / Inf [1] NaN # Testing for Inf is.infinite(Inf) [1] TRUE is.finite(Inf) [1] FALSE

NA vs NaN vs Inf: Key Distinctions

Property NA NaN Inf
Meaning Missing data Undefined math Infinity
is.na() TRUE TRUE FALSE
is.nan() FALSE TRUE FALSE
is.finite() FALSE FALSE FALSE
is.infinite() FALSE FALSE TRUE
\( x + 5 \) NA NaN Inf

The Testing Function Hierarchy

is.na() == TRUE is.nan() == TRUE NaN
\( 0/0, \sqrt{-1} \)
NA Missing data Inf
\( 1/0 \)
is.infinite() == TRUE is.finite() returns TRUE only for regular numbers

How to Handle Special Values

1
Detect: Use is.na(x), is.nan(x), is.infinite(x) to identify which special values you have
2
Count: Use sum(is.na(x)) to see how many NAs are in your vector
3
Remove or skip: Use na.rm = TRUE in functions like mean(), sum(), max()
4
Replace: Use x[is.na(x)] <- 0 or ifelse(is.na(x), 0, x) to substitute values
5
Filter: Use x[is.finite(x)] to keep only normal, finite numbers

Example 1: Detecting Special Values

# Create a vector with special values x <- c(4, NA, 7, NaN, Inf, 2) # Test each element is.na(x) [1] FALSE TRUE FALSE TRUE FALSE FALSE is.nan(x) [1] FALSE FALSE FALSE TRUE FALSE FALSE is.infinite(x) [1] FALSE FALSE FALSE FALSE TRUE FALSE is.finite(x) [1] TRUE FALSE TRUE FALSE FALSE TRUE

Example 2: Using na.rm

# A student's test scores (one missing) scores <- c(85, 92, NA, 78, 95) # Without na.rm: infection! mean(scores) [1] NA sum(scores) [1] NA # With na.rm = TRUE: the cure mean(scores, na.rm = TRUE) [1] 87.5 sum(scores, na.rm = TRUE) [1] 350 max(scores, na.rm = TRUE) [1] 95

Example 3: Replacing and Filtering

# Sensor data with problems temps <- c(22.1, NA, 23.5, Inf, 21.8, NaN) # Strategy 1: Replace NA/NaN with 0 temps_clean <- temps temps_clean[is.na(temps_clean)] <- 0 temps_clean [1] 22.1 0.0 23.5 Inf 21.8 0.0 # Strategy 2: Keep only finite values temps_finite <- temps[is.finite(temps)] temps_finite [1] 22.1 23.5 21.8 # Strategy 3: Mean of finite values # Note: na.rm=TRUE alone is not enough, as it keeps Inf mean(temps, na.rm = TRUE) [1] Inf # Correct approach: filter with is.finite() first mean(temps[is.finite(temps)]) [1] 22.46667

Typed NA Values

R's NA is actually a logical value by default. But R also has typed NAs for each atomic type, which matter when building vectors or data frames with strict types.

# Default NA is logical class(NA) [1] "logical" # Typed NAs NA_integer_ # integer NA NA_real_ # double (numeric) NA NA_complex_ # complex NA NA_character_ # character NA # Why this matters x <- vector("integer", 3) x[1] <- NA_integer_ # preserves type x[2] <- NA # also works (coerced)

Common Mistakes

✕ Using \(==\) to test NA
\( x == \text{NA} \) returns NA, not TRUE/FALSE. Always use is.na(x) instead.
✕ Forgetting na.rm
Functions like mean(), sum(), sd() return NA if any element is NA. Add na.rm = TRUE.
✕ Assuming na.rm removes Inf
na.rm = TRUE only removes NA and NaN, not Inf. Filter with is.finite() to exclude Inf too.

Summary Reference Table

Task R Code Returns
Check for NA is.na(x) Logical vector
Check for NaN is.nan(x) Logical vector
Check for Inf is.infinite(x) Logical vector
Check for finite is.finite(x) Logical vector
Count NAs sum(is.na(x)) Integer
Remove NAs na.omit(x) Cleaned vector
Skip NAs in calc mean(x, na.rm=TRUE) Numeric
Replace NAs x[is.na(x)] <- 0 Modified vector

Question 1 of 5

What does is.na(NaN) return in R?

Question 2 of 5

What is the result of \( \text{NA} == \text{NA} \) in R?

Question 3 of 5

Given x <- c(3, NA, Inf, 7), what does mean(x, na.rm = TRUE) return?

Question 4 of 5

Which R expression produces NaN?

Question 5 of 5

You want to keep only normal, finite numbers from a vector that may contain NA, NaN, and Inf. Which approach works?