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?
-
7I'd say NA is more of a "placeholder"; NaN is for (IEEE) arithmetic purposes. – J. M. is not a statistician Dec 22 '10 at 07:04
-
@JM. good way to summarize. – suncoolsu Dec 22 '10 at 07:09
-
and there is also Inf, which stands for expressions like for instance 1/0... – Karsten W. Dec 22 '10 at 08:43
-
It's explained in the documentation [here](http://cran.r-project.org/doc/manuals/r-release/R-intro.html#Missing-values). – gung - Reinstate Monica Mar 15 '13 at 13:36
-
1I think this question is better suited for stack overflow, but the question is too old to migrate. – Zach Mar 15 '13 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.
-
21Of note, `is.na()` returns `TRUE` for both NA and NaN, which differs from `is.nan()` e.g. `is.na(c(0/0,NA))` *vs.* `is.nan(c(0/0,NA))`. – chl Dec 23 '10 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

- 33,140
- 5
- 82
- 138
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.

- 10,388
- 7
- 50
- 93
-
-
-
2Several language have equivalent constructs. For instance PHP and Javascript have null and NaN. – nico Dec 23 '10 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.
:-)

- 2,084
- 18
- 31
-
3
-
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. – f1r3br4nd Oct 24 '16 at 19:14
NA = Not Available
NaN = Not a Number
I think once we expand the acronyms, it should be self explanatory.

- 101
- 1
-
2Welcome to CV! Thanks for answering, but do please read existing answers first, & consider whether you're adding anything new. – Scortchi - Reinstate Monica Jan 26 '15 at 14:42