Understanding NA in R
Learn what NA means, why it propagates, and how it differs from NULL.
What is NA in R?
NA stands for Not Available — it represents a missing or unknown value. Unlike NULL (absence of an object) or NaN (not a number), NA is a placeholder for data that exists but is unknown.
# NA represents a missing value
height_cm <- c(175, NA, 162, 188, NA, 170)
cat('Heights:', height_cm, '
')
cat('Length:', length(height_cm), '
') # NA counts as an element
cat('Sum:', sum(height_cm), '
') # NA propagatesNA vs NULL
NA is a missing value inside a vector — the slot exists but has no known value. NULL is the absence of an object entirely — it has length 0 and cannot be stored inside a vector.
# NA: element exists, value unknown
vec_with_na <- c(1, NA, 3)
cat('Vector with NA, length:', length(vec_with_na), '
') # 3
# NULL: nothing there at all
vec_with_null <- c(1, NULL, 3)
cat('Vector with NULL, length:', length(vec_with_null), '
') # 2!
# NULL gets dropped; NA is kept
cat('vec_with_na:', vec_with_na, '
')
cat('vec_with_null:', vec_with_null, '
')