Examples for 'base::NA'


'Not Available' / Missing Values

Aliases: NA NA_integer_ NA_real_ NA_complex_ NA_character_ is.na is.na.data.frame is.na<- is.na<-.default anyNA anyNA.data.frame anyMissing

Keywords: NA logic manip

### ** Examples

is.na(c(1, NA))        #> FALSE  TRUE
[1] FALSE  TRUE
is.na(paste(c(1, NA))) #> FALSE FALSE
[1] FALSE FALSE
(xx <- c(0:4))
[1] 0 1 2 3 4
is.na(xx) <- c(2, 4)
xx                     #> 0 NA  2 NA  4
[1]  0 NA  2 NA  4
anyNA(xx) # TRUE
[1] TRUE
# Some logical operations do not return NA
c(TRUE, FALSE) & NA
[1]    NA FALSE
c(TRUE, FALSE) | NA
[1] TRUE   NA
## No test: 
## 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)))))
}
Loading required package: microbenchmark
Warning in library(package, lib.loc = lib.loc, character.only = TRUE,
logical.return = TRUE, : there is no package called 'microbenchmark'
      user.self sys.self elapsed user.child sys.child
is.na     0.139    0.153   0.381          0         0
anyNA     0.027    0.001   0.031          0         0
## End(No test)

## 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))
[1] FALSE  TRUE FALSE  TRUE
[1] FALSE  TRUE
sapply(L2, anyNA); c(anyNA(L2), anyNA(L2, TRUE))
[1] FALSE FALSE
[1] FALSE FALSE
## ... lists, and hence data frames, too:
dN <- dd <- USJudgeRatings; dN[3,6] <- NA
anyNA(dd) # FALSE
[1] FALSE
anyNA(dN) # TRUE
[1] TRUE

[Package base version 4.2.3 Index]