<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/the-trick-to-understanding-nas-missing-values-in-r/"></div></div><div><a data-url="http://www.r-bloggers.com/the-trick-to-understanding-nas-missing-values-in-r/" data-counturl="http://www.r-bloggers.com/the-trick-to-understanding-nas-missing-values-in-r/" data-count="horizontal" data-text="The trick to understanding NAs (missing values) in R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.revolutionanalytics.com/2016/07/understanding-na-in-r.html"> Revolutions</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p>Here's a little puzzle that might shed some light on some apparently confusing behaviour by missing values (<span>NA</span>s) in R:</p>
<p>What is <span>NA^0</span> in R?</p>
<p>You can get the answer easily by typing at the R command line:</p>
<p><span>> NA^0</span><br><span>[1] 1</span></p>
<p>But the interesting question that arises is: why is it 1? Most people might expect that the answer would be <span>NA</span>, like most expressions that include <span>NA</span>. But here's the trick to understanding this outcome: think of NA not as a number, but as a <strong>placeholder</strong> for a number that exists, but whose value we don't know. </p>
<p>Now think of all of the numbers that could replace <span>NA</span> in the expression <span>NA^0</span>. Any positive number to the power zero is 1. Same goes for any negative number. Even zero to the power zero is defined by mathematicians to be 1 (for <a target="_blank" href="http://mathforum.org/dr.math/faq/faq.0.to.0.power.html">reasons</a> I'm not going to go into here). So that means <strong>whatever</strong> number you substitute for <span>NA</span> in the expression <span>NA^0</span>, the answer will be 1. And so that's the answer R gives.</p>
<p>There are a few other instances where using the indeterminate <span>NA</span> in an expression can lead to a specific non-<span>NA</span> result. Consider this example:</p>
<p><span>> NA || TRUE</span><br><span>[1] TRUE</span></p>
<p>Here. the <span>NA</span> is holding the place of a logical value<sup>1</sup>, so it could be representing only <span>TRUE</span> or <span>FALSE</span>. But whatever it represents, the answer will be the same:</p>
<p><span>> TRUE || TRUE</span><br><span>[1] TRUE</span><br><span>> FALSE || TRUE</span><br><span>[1] TRUE</span></p>
<p>By the same token, <span>any(x)</span> can return <span>TRUE</span> even if the logical vector includes <span>NA</span>s, as long as <span>x</span> includes at least one <span>TRUE</span> value. Similarly, <span>NA &amp;&amp; FALSE</span> is always <span>FALSE</span>.</p>
<p>There are a few other examples as well (if you know some, share them in the comments). But always remember: if you're ever confused by the behaviour of <span>NA</span> in R, think about what values it might contain, and if changing them changes the outcome. That might explain what's going on. For more on how R handles NAs, see the <a target="_blank" href="https://cran.r-project.org/doc/manuals/r-release/R-lang.html#NA-handling">R Language Definition</a>.</p>
<p><sup>1</sup><strong>Footnote</strong>: I'm deliberately ignoring the storage mode of <span>NA</span>, which can come in logical, integer, double and character flavours. In all the examples above, it gets coerced to the type of the other elements in the expression.</p>
<p> </p>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/the-trick-to-understanding-nas-missing-values-in-r/"></div></div><div><a data-url="http://www.r-bloggers.com/the-trick-to-understanding-nas-missing-values-in-r/" data-counturl="http://www.r-bloggers.com/the-trick-to-understanding-nas-missing-values-in-r/" data-count="vertical" data-text="The trick to understanding NAs (missing values) in R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://blog.revolutionanalytics.com/2016/07/understanding-na-in-r.html"> Revolutions</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/YyeKTTPrf9k"></a></div>