54

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?

user2479
  • 641
  • 1
  • 5
  • 3

5 Answers5

45

?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.

suncoolsu
  • 6,202
  • 30
  • 46
  • 21
    Of 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
12

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 
mpiktas
  • 33,140
  • 5
  • 82
  • 138
6

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.

shabbychef
  • 10,388
  • 7
  • 50
  • 93
2

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.

:-)

f1r3br4nd
  • 2,084
  • 18
  • 31
  • 3
    what are the first two? – David LeBauer Aug 17 '13 at 21:17
  • 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
0

NA = Not Available

NaN = Not a Number

I think once we expand the acronyms, it should be self explanatory.