I would like to know why some languages like R has both NA and NaN. What are the differences or are they equally the same? Is it really needed to have NA?
-
7$\begingroup$ I'd say NA is more of a "placeholder"; NaN is for (IEEE) arithmetic purposes. $\endgroup$– J. M. is not a statisticianDec 22, 2010 at 7:04
-
$\begingroup$ @JM. good way to summarize. $\endgroup$– suncoolsuDec 22, 2010 at 7:09
-
$\begingroup$ and there is also Inf, which stands for expressions like for instance 1/0... $\endgroup$– Karsten W.Dec 22, 2010 at 8:43
-
$\begingroup$ It's explained in the documentation here. $\endgroup$– gung - Reinstate MonicaMar 15, 2013 at 13:36
-
1$\begingroup$ I think this question is better suited for stack overflow, but the question is too old to migrate. $\endgroup$– ZachMar 15, 2013 at 13:48
5 Answers
?is.nan
?is.na
?NA
?NaN
Should answer your question.
But, in short:
NaN means $\frac {0} {0}$ -- Stands for Not a Number
NA is generally interpreted as a missing value and has various forms - NA_integer_, NA_real_, etc.
Therefore, NaN $\neq$ NA and there is a need for NaN and NA.
-
21$\begingroup$ Of note,
is.na()
returnsTRUE
for both NA and NaN, which differs fromis.nan()
e.g.is.na(c(0/0,NA))
vs.is.nan(c(0/0,NA))
. $\endgroup$– chlDec 23, 2010 at 18:04
NA is for missing data. NaN, as J.M. said is for arithmetic purpose. NaN is usually the product of some arithmetic operation, such as 0/0
. NA usually is declared in advance, or is a product of operation when you try to access something that is not there:
> a <- c(1,2)
> a[3]
[1] NA
I think of NA standing for 'Not Available', while NaN is 'Not a Number', although this is more mnemonic than explanation. By the way, I know of no language other than R (perhaps Splus?) that has both. Matlab, for example, has only NaN.
-
$\begingroup$ You made a (critical) typo. "NaN is 'Not a Number'" $\endgroup$ Dec 23, 2010 at 11:22
-
$\begingroup$ @Peter Smit: ouch, thanks. my X server is lagging keystrokes... $\endgroup$ Dec 23, 2010 at 17:23
-
2$\begingroup$ Several language have equivalent constructs. For instance PHP and Javascript have null and NaN. $\endgroup$– nicoDec 23, 2010 at 17:55
NA means the error was already there when you imported the spreadsheet into R. NaN means you caused the error after importing the data. It's the third type of error that's really hard to catch.
-
3
-
$\begingroup$ I was being glib, but what I meant was-- NA is the first type of error, often caused by something in the imported data being the wrong type-- e.g. a numeric field containing punctuation/letters/whitespace or typos/case-variation in the levels of a factor. NaN is the second error, that more often happens when you transform data within R. The third error is data that does have a numeric value, but for one reason or another the wrong one, and it's less immediately noticeable. Again, this is not meant to be a rigorous categorization, just an informal observation. $\endgroup$ Oct 24, 2016 at 19:14
NA = Not Available
NaN = Not a Number
I think once we expand the acronyms, it should be self explanatory.
-
3$\begingroup$ Welcome to CV! Thanks for answering, but do please read existing answers first, & consider whether you're adding anything new. $\endgroup$ Jan 26, 2015 at 14:42