Rev 62675 | Rev 65263 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/NA.Rd% Part of the R package, http://www.R-project.org% Copyright 1995-2013 R Core Team% Distributed under GPL 2 or later\name{NA}\alias{NA}\alias{NA_integer_}\alias{NA_real_}\alias{NA_complex_}\alias{NA_character_}\alias{is.na}\alias{is.na.data.frame}\alias{is.na<-}\alias{is.na<-.default}\alias{anyNA}\alias{anyMissing}% an alternative name (in Biobase and S+ as of ~2006)\title{\sQuote{Not Available} / Missing Values}\description{\code{NA} is a logical constant of length 1 which contains a missingvalue indicator. \code{NA} can be coerced to any other vectortype except raw. There are also constants \code{NA_integer_},\code{NA_real_}, \code{NA_complex_} and \code{NA_character_} of theother atomic vector types which support missing values: all of theseare \link{reserved} words in the \R language.The generic function \code{is.na} indicates which elements are missing.The generic function \code{is.na<-} sets elements to \code{NA}.The generic function \code{anyNA} with its default method implements\code{any(is.na(x))} in a fast way, not dispatching methods for\code{is.na} and \code{any}.}\usage{NAis.na(x)anyNA(x)\method{is.na}{data.frame}(x)is.na(x) <- value}\arguments{\item{x}{an \R object to be tested: the default method handles atomicvectors, lists and pairlists.}\item{value}{a suitable index vector for use with \code{x}.}}%% Next par on character NAs taken from R-lang.texi:\details{The \code{NA} of character type is distinct from thestring \code{"NA"}. Programmers who need to specify an explicitstring \code{NA} should use \code{NA_character_} rather than\code{"NA"}, or set elements to \code{NA} using \code{is.na<-}.\code{is.na(x)} works elementwise when \code{x} is a\code{\link{list}}. It is generic: you can write methods to handlespecific classes of objects, see \link{InternalMethods}. A complexvalue is regarded as \code{NA} if either its real or imaginary part is\code{NA} or \code{NaN}.Function \code{is.na<-} may provide a safer way to set missingness.It behaves differently for factors, for example.Computations using \code{NA} will normally result in \code{NA}: apossible exception is where \code{\link{NaN}} is also involved, inwhich case either might result.}\value{The default method for \code{is.na} applied to an atomic vectorreturns a logical vector of the same length as its argument \code{x},containing \code{TRUE} for those elements marked \code{NA} or, fornumeric or complex vectors, \code{\link{NaN}} (!) and \code{FALSE}otherwise. \code{dim}, \code{dimnames} and \code{names} attributesare preserved.The default methods also works for lists and pairlists:\crFor \code{is.na}, elementwise the result is false unless that elementis a length-one atomic vector and the single element of that vector isregarded as \code{NA} or \code{NaN} (note that any \code{is.na}methods for the class of the atomic vector is ignored).For \code{anyNA}, such lists are treated \emph{recursively}, suchthat the result is only false if there is no \code{NA} or \code{NaN} in anyelement (which may be a list itself).The data frame method for \code{is.na} returns a logical matrixwith the same dimensions as the data frame, and with dimnames takenfrom the row and column names of the data frame.}\references{Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)\emph{The New S Language}.Wadsworth & Brooks/Cole.Chambers, J. M. (1998)\emph{Programming with Data. A Guide to the S Language}.Springer.}\seealso{\code{\link{NaN}}, \code{\link{is.nan}}, etc.,and the utility function \code{\link{complete.cases}}.\code{\link{na.action}}, \code{\link{na.omit}}, \code{\link{na.fail}}on how methods can be tuned to deal with missing values.}\examples{is.na(c(1, NA)) #> FALSE TRUEis.na(paste(c(1, NA))) #> FALSE FALSE(xx <- c(0:4))is.na(xx) <- c(2, 4)xx #> 0 NA 2 NA 4anyNA(xx) # TRUE\donttest{## Measure speed difference in an extreme case:if(require("microbenchmark")) { # does not work reliably on all platformsx <- c(NaN, 1:10000)print(microbenchmark(any(is.na(x)), anyNA(x)))} else { ## much less accuratex <- c(NaN, 1e6)nSim <- 2^13print(rbind(is.na = system.time(replicate(nSim, any(is.na(x)))),anyNA = system.time(replicate(nSim, anyNA(x)))))}}## anyNA() works 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); anyNA(LL)sapply(L2, anyNA); anyNA(L2)## ... lists, and hence data frames, too; no method needed:dN <- dd <- USJudgeRatings; dN[3,6] <- NAanyNA(dd) # FALSEanyNA(dN) # TRUE}\keyword{NA}\keyword{logic}\keyword{manip}