NA

0th

Percentile

‘Not Available’ / Missing Values

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<- sets elements to NA.

The generic function anyNA implements any(is.na(x)) in a possibly faster way (especially for atomic vectors).

Keywords
manip, logic, NA
Usage
NA
is.na(x)
anyNA(x, recursive = FALSE)# S3 method for data.frame
is.na(x)is.na(x) <- value
Arguments
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.

Details

The NA of character type is distinct from the string "NA". Programmers who need to specify an explicit missing string should use NA_character_ (rather than "NA") or set elements to NA using is.na<-.

is.na and anyNA are generic: you can write methods to handle specific classes of objects, see InternalMethods.

Function is.na<- may provide a safer way to set missingness. It behaves differently for factors, for example.

Numerical computations using NA will normally result in NA: a possible exception is where NaN is also involved, in which case either might result (which may depend on the R platform). Logical computations treat NA as a missing TRUE/FALSE value, and so may return TRUE or FALSE if the expression does not depend on the NA operand.

The default method for anyNA handles atomic vectors without a class and NULL. It calls any(is.na(x)) on objects with classes and for recursive = FALSE, on lists and pairlists.

Value

The default method for is.na applied to an atomic vector returns a logical vector of the same length as its argument x, containing TRUE for those elements marked NA or, for numeric or complex vectors, NaN, and FALSE otherwise. (A complex value is regarded as NA if either its real or imaginary part is NA or NaN.) dim, dimnames and names attributes are copied to the result.

The default methods also work for lists and pairlists: For is.na, elementwise the result is false unless that element is a length-one atomic vector and the single element of that vector is regarded as NA or NaN (note that any is.na method for the class of the element is ignored). anyNA(recursive = FALSE) works the same way as is.na; anyNA(recursive = TRUE) applies anyNA (with method dispatch) to each element.

The data frame method for is.na returns a logical matrix with the same dimensions as the data frame, and with dimnames taken from the row and column names of the data frame.

anyNA(NULL) is false; is.na(NULL) is logical(0) (no longer warning since R version 3.5.0).

References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

Chambers, J. M. (1998) Programming with Data. A Guide to the S Language. Springer.

NaN, is.nan, etc., and the utility function complete.cases.

na.action, na.omit, na.fail on how methods can be tuned to deal with missing values.

Aliases
• NA
• NA_integer_
• NA_real_
• NA_complex_
• NA_character_
• is.na
• is.na.data.frame
• is.na<-
• is.na<-.default
• anyNA
• anyMissing
Examples
library(base) # 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 <- 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 # } 
Documentation reproduced from package base, version 3.6.0, License: Part of R 3.6.0

Community examples

mark@niemannross.com at Jan 2, 2019 base v3.5.2

[link to LinkedIn Learning Course:](https://linkedin-learning.pxf.io/rweekly_na) r ?NA is.na(NA) is.na(NaN) is.na("NA") # "NA" is a string # test contents of a vector test_vector <- c(1,2,3,NA,5) is.na(test_vector) anyNA(test_vector) # is there an NA in the data? mean(test_vector) # many functions have built-in NA handling mean(test_vector, na.rm = TRUE) # ways to convert NA to zero ifelse(is.na(test_vector),0,test_vector) test_vector[is.na(test_vector)] <- 0 # other useful NA tools na.fail(test_vector) na.omit(test_vector) # remove NA, returning index to items removed