R Missing Values
Real data has holes: a sensor failed, a survey question was skipped, a value was never recorded. R represents a missing value with NA ("not available"), and it treats NA carefully so you can't accidentally ignore it.
temps <- c(21.5, NA, 23.1, 19.8, NA)
temps
length(temps)
is.na(temps)
sum(is.na(temps))[1] 21.5 NA 23.1 19.8 NA
[1] 5
[1] FALSE TRUE FALSE FALSE TRUE
[1] 2
NA spreads through calculations
Any calculation involving an unknown value gives an unknown result. That includes comparisons, so you can't test for NA with ==:
NA + 1
mean(c(4, NA, 6))
NA > 3
NA == NA # unknown, not TRUE!
is.na(NA) # the right way[1] NA
[1] NA
[1] NA
[1] NA
[1] TRUE
na.rm = TRUE
Most summary functions take na.rm = TRUE to remove missing values before computing:
temps <- c(21.5, NA, 23.1, 19.8, NA)
mean(temps, na.rm = TRUE)
max(temps, na.rm = TRUE)
sum(!is.na(temps)) # how many values we actually have[1] 21.46667
[1] 23.1
[1] 3
Finding and removing missing values
temps <- c(21.5, NA, 23.1, 19.8, NA)
which(is.na(temps))
temps[!is.na(temps)]
na.omit(temps)[1] 2 5
[1] 21.5 23.1 19.8
[1] 21.5 23.1 19.8
attr(,"na.action")
[1] 2 5
attr(,"class")
[1] "omit"
na.omit() also records which positions it dropped (the na.action attribute), which modelling functions use.
Missing values in data frames
airquality has gaps in the Ozone and Solar.R columns. complete.cases() flags rows with no NA at all:
colSums(is.na(airquality))
nrow(airquality)
sum(complete.cases(airquality))
clean <- na.omit(airquality)
nrow(clean)
head(airquality[!complete.cases(airquality), ], 4) Ozone Solar.R Wind Temp Month Day
37 7 0 0 0 0
[1] 153
[1] 111
[1] 111
Ozone Solar.R Wind Temp Month Day
5 NA NA 14.3 56 5 5
6 28 NA 14.9 66 5 6
10 NA 194 8.6 69 5 10
11 7 NA 6.9 74 5 11
Dropping incomplete rows is simple, but you lose data: here 42 of 153 days disappear.
Replacing missing values
Sometimes it makes sense to fill gaps (imputation), for example with the mean or median of the column, or a fixed value. Do it consciously, because it changes your data:
oz <- airquality$Ozone
med <- median(oz, na.rm = TRUE)
oz_filled <- ifelse(is.na(oz), med, oz)
sum(is.na(oz_filled))
c(before = mean(oz, na.rm = TRUE), after = mean(oz_filled))
scores <- c(8, NA, 7, NA, 10)
scores[is.na(scores)] <- 0
scores[1] 0
before after
42.12931 39.55882
[1] 8 0 7 0 10
Filling 37 gaps with the median (31.5) pulls the mean down from about 42.1 to 39.6, and it also makes the data look less variable than it really is. That is why imputation needs care.
NA in factors, tables and models
table() ignores NA unless you ask; useNA = "ifany" shows the count. Models like lm() drop incomplete rows automatically and tell you how many:
answers <- c("yes", NA, "no", "yes", NA)
table(answers)
table(answers, useNA = "ifany")
fit <- lm(Ozone ~ Temp, data = airquality)
nobs(fit)answers
no yes
1 2
answers
no yes <NA>
1 2 2
[1] 116
NA, NULL and NaN
| Value | Meaning | Test |
|---|---|---|
NA |
a value exists but is unknown | is.na() |
NaN |
the result of an undefined calculation (0/0) |
is.nan() (also is.na()) |
NULL |
nothing at all, length 0 | is.null() |
x <- c(1, NA, NaN)
is.na(x)
is.nan(x)
length(NULL)
length(NA)
c(1, NULL, 3)[1] FALSE TRUE TRUE
[1] FALSE FALSE TRUE
[1] 0
[1] 1
[1] 1 3
There are typed versions of NA too (NA_integer_, NA_real_, NA_character_) for when the type matters.
Test yourself
What is mean(c(2, NA, 4))?
- 3
- NA
- 2
- An error
How do you test whether x is missing?
- x == NA
- is.na(x)
- x == NULL
- missing(x)
Which function returns TRUE for rows of a data frame without any NA?
- na.omit()
- complete.cases()
- is.na()
- anyNA()