EVOLUTION-MANAGER
Edit File: NA.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: 'Not Available' / Missing Values</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for NA {base}"><tr><td>NA {base}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>‘Not Available’ / Missing Values</h2> <h3>Description</h3> <p><code>NA</code> is a logical constant of length 1 which contains a missing value indicator. <code>NA</code> can be coerced to any other vector type except raw. There are also constants <code>NA_integer_</code>, <code>NA_real_</code>, <code>NA_complex_</code> and <code>NA_character_</code> of the other atomic vector types which support missing values: all of these are <a href="Reserved.html">reserved</a> words in the <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> language. </p> <p>The generic function <code>is.na</code> indicates which elements are missing. </p> <p>The generic function <code>is.na<-</code> sets elements to <code>NA</code>. </p> <p>The generic function <code>anyNA</code> implements <code>any(is.na(x))</code> in a possibly faster way (especially for atomic vectors). </p> <h3>Usage</h3> <pre> NA is.na(x) anyNA(x, recursive = FALSE) ## S3 method for class 'data.frame' is.na(x) is.na(x) <- value </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>an <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> object to be tested: the default method for <code>is.na</code> and <code>anyNA</code> handle atomic vectors, lists, pairlists, and <code>NULL</code>.</p> </td></tr> <tr valign="top"><td><code>recursive</code></td> <td> <p>logical: should <code>anyNA</code> be applied recursively to lists and pairlists?</p> </td></tr> <tr valign="top"><td><code>value</code></td> <td> <p>a suitable index vector for use with <code>x</code>.</p> </td></tr> </table> <h3>Details</h3> <p>The <code>NA</code> of character type is distinct from the string <code>"NA"</code>. Programmers who need to specify an explicit missing string should use <code>NA_character_</code> (rather than <code>"NA"</code>) or set elements to <code>NA</code> using <code>is.na<-</code>. </p> <p><code>is.na</code> and <code>anyNA</code> are generic: you can write methods to handle specific classes of objects, see <a href="InternalMethods.html">InternalMethods</a>. </p> <p>Function <code>is.na<-</code> may provide a safer way to set missingness. It behaves differently for factors, for example. </p> <p>Numerical computations using <code>NA</code> will normally result in <code>NA</code>: a possible exception is where <code><a href="is.finite.html">NaN</a></code> is also involved, in which case either might result (which may depend on the <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> platform). Logical computations treat <code>NA</code> as a missing <code>TRUE/FALSE</code> value, and so may return <code>TRUE</code> or <code>FALSE</code> if the expression does not depend on the <code>NA</code> operand. </p> <p>The default method for <code>anyNA</code> handles atomic vectors without a class and <code>NULL</code>. It calls <code>any(is.na(x))</code> on objects with classes and for <code>recursive = FALSE</code>, on lists and pairlists. </p> <h3>Value</h3> <p>The default method for <code>is.na</code> applied to an atomic vector returns a logical vector of the same length as its argument <code>x</code>, containing <code>TRUE</code> for those elements marked <code>NA</code> or, for numeric or complex vectors, <code><a href="is.finite.html">NaN</a></code>, and <code>FALSE</code> otherwise. (A complex value is regarded as <code>NA</code> if either its real or imaginary part is <code>NA</code> or <code><a href="is.finite.html">NaN</a></code>.) <code>dim</code>, <code>dimnames</code> and <code>names</code> attributes are copied to the result. </p> <p>The default methods also work for lists and pairlists:<br /> For <code>is.na</code>, elementwise the result is false unless that element is a length-one atomic vector and the single element of that vector is regarded as <code>NA</code> or <code>NaN</code> (note that any <code>is.na</code> method for the class of the element is ignored).<br /> <code>anyNA(recursive = FALSE)</code> works the same way as <code>is.na</code>; <code>anyNA(recursive = TRUE)</code> applies <code>anyNA</code> (with method dispatch) to each element. </p> <p>The data frame method for <code>is.na</code> 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. </p> <p><code>anyNA(NULL)</code> is false; <code>is.na(NULL)</code> is <code>logical(0)</code> (no longer warning since <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> version 3.5.0). </p> <h3>References</h3> <p>Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) <em>The New S Language</em>. Wadsworth & Brooks/Cole. </p> <p>Chambers, J. M. (1998) <em>Programming with Data. A Guide to the S Language</em>. Springer. </p> <h3>See Also</h3> <p><code><a href="is.finite.html">NaN</a></code>, <code><a href="is.finite.html">is.nan</a></code>, etc., and the utility function <code><a href="../../stats/html/complete.cases.html">complete.cases</a></code>. </p> <p><code><a href="../../stats/html/na.action.html">na.action</a></code>, <code><a href="../../stats/html/na.fail.html">na.omit</a></code>, <code><a href="../../stats/html/na.fail.html">na.fail</a></code> on how methods can be tuned to deal with missing values. </p> <h3>Examples</h3> <pre> 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 ## 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))))) } ## 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 </pre> <hr /><div style="text-align: center;">[Package <em>base</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>