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.
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:
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.
mean() return NA by defaultTRUEWhere Do They Come From?
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.
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.
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.
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
How to Handle Special Values
is.na(x), is.nan(x), is.infinite(x) to identify which special values you havesum(is.na(x)) to see how many NAs are in your vectorna.rm = TRUE in functions like mean(), sum(), max()x[is.na(x)] <- 0 or ifelse(is.na(x), 0, x) to substitute valuesx[is.finite(x)] to keep only normal, finite numbersExample 1: Detecting Special Values
Example 2: Using na.rm
Example 3: Replacing and Filtering
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.
Common Mistakes
mean(), sum(), sd() return NA if any element is NA. Add na.rm = TRUE.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?
is.na(NaN) returns TRUE. To specifically detect NaN (and not other NAs), use is.nan().Question 2 of 5
What is the result of \( \text{NA} == \text{NA} \) in R?
is.na() instead of \( == \text{NA} \) to test for missing values.Question 3 of 5
Given x <- c(3, NA, Inf, 7), what does mean(x, na.rm = TRUE) return?
na.rm = TRUE, the NA is removed, leaving c(3, Inf, 7). Since Inf is not NA, it stays in the calculation. The mean of any set containing Inf is Inf. To exclude Inf, filter with is.finite() first.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?
!is.na(x) and na.omit(x) remove NA and NaN but keep Inf. !is.nan(x) removes only NaN. Only is.finite(x) returns TRUE exclusively for regular finite numbers, filtering out all three: NA, NaN, and Inf.