NA is a logical constant of length 1 which contains a missing value indicator. NA can be coerced to any other vector type except raw. There are also constants NA_integer_, NA_real_, NA_complex_ and NA_character_ of the other atomic vector types which support missing values: all of these are reserved words in the R language. The generic function is.na indicates which elements are missing. The generic function is.na
NA
is.na(x)
anyNA(x, recursive = FALSE)<p></p><p># S3 method for data.frame
is.na(x)</p><p>is.na(x) <- value</p>
| Parameter | Description |
|---|---|
x |
an R object to be tested: the default method for is.na and anyNA handle atomic vectors, lists, pairlists, and NULL. |
recursive |
logical: should anyNA be applied recursively to lists and pairlists? |
value |
a suitable index vector for use with x. |
# NOT RUN {
is.na(c(1, NA)) #> FALSE TRUE
is.na(paste(c(1, NA))) #> FALSE FALSE
(xx <- c(0:4))
is.na(xx) <- c(2, 4)
xx #> 0 NA 2 NA 4
anyNA(xx) # TRUE
# Some logical operations do not return NA
c(TRUE, FALSE) & NA
c(TRUE, FALSE) | NA
# }
# NOT RUN {
## Measure speed difference in a favourable case:
## the difference depends on the platform, on most ca 3x.
x <- 1:10000; x[5000] <- NaN # coerces x to be double
if(require("microbenchmark")) { # does not work reliably on all platforms
print(microbenchmark(any(is.na(x)), anyNA(x)))
} else {
nSim <- 2^13
print(rbind(is.na = system.time(replicate(nSim, any(is.na(x)))),
anyNA = system.time(replicate(nSim, anyNA(x)))))
}
# }
# NOT RUN {
## anyNA() can work recursively with list()s:
LL <- list(1:5, c(NA, 5:8), c("A","NA"), c("a", NA_character_))
L2 <- LL[c(1,3)]
sapply(LL, anyNA); c(anyNA(LL), anyNA(LL, TRUE))
sapply(L2, anyNA); c(anyNA(L2), anyNA(L2, TRUE))
## ... lists, and hence data frames, too:
dN <- dd <- USJudgeRatings; dN[3,6] <- NA
anyNA(dd) # FALSE
anyNA(dN) # TRUE
# }