Missing Values: NA and NULL
Prerequisites: Logical values and comparisons
Real data has gaps: a survey question left blank, a sensor that failed,
a value not yet measured. R represents a missing value with NA
("not available").
NA spreads through calculations
heights <- c(162, NA, 181, 158)
heights + 1
mean(heights)
mean(heights, na.rm = TRUE)
[1] 163 NA 182 159
[1] NA
[1] 167
Any calculation involving an unknown value is unknown — so the mean is
NA. na.rm = TRUE ("remove NAs") tells the function to ignore missing
values. Most summary functions accept it: sum, min, max, sd...
Finding missing values
heights <- c(162, NA, 181, NA)
is.na(heights)
sum(is.na(heights))
heights[!is.na(heights)]
heights == NA
[1] FALSE TRUE FALSE TRUE
[1] 2
[1] 162 181
[1] NA NA NA NA
heights == NA doesn't work — comparing anything with an unknown value
gives NA. Always use is.na() to test for missing values.
NULL means "nothing"
NULL is different from NA: it means the value doesn't exist at all,
rather than being missing.
length(NA)
length(NULL)
c(1, NULL, 2)
[1] 1
[1] 0
[1] 1 2
NA is a placeholder that takes up a slot; NULL disappears. You'll
mostly meet NULL as the result of looking up something that doesn't
exist, such as a missing list element.
Common mistakes
- Forgetting
na.rm = TRUEand gettingNAfor a whole summary. - Silently dropping missing values without asking why they're missing. Always count them first — how much data is missing, and where, can change your conclusions.
Practice
Given temps <- c(21, NA, 25, 19, NA, 23), print how many values are
missing and the average of the rest.
Show solution
temps <- c(21, NA, 25, 19, NA, 23)
sum(is.na(temps))
mean(temps, na.rm = TRUE)
[1] 2
[1] 22