anyNA
reports whether any missing values are present; while is.na
reports missing values elementwise:
vec <- c(1, 2, 3, NA, 5)
anyNA(vec)
# [1] TRUE
is.na(vec)
# [1] FALSE FALSE FALSE TRUE FALSE
ìs.na
returns a logical vector that is coerced to integer values under arithmetic operations (with FALSE=0, TRUE=1). We can use this to find out how many missing values there are:
sum(is.na(vec))
# [1] 1
Extending this approach, we can use colSums
and is.na
on a data frame to count NAs per column:
colSums(is.na(airquality))
# Ozone Solar.R Wind Temp Month Day
# 37 7 0 0 0 0
The naniar package (currently on github but not CRAN) offers further tools for exploring missing values.