32

What is the difference between NaN and Inf, and NULL and NA in R?

Why ?NA and ?NULL tell me that "NA" has a length of "1" whereas NULL has a length of "0"?

Igor Chubin
  • 61,765
  • 13
  • 122
  • 144
  • NaN vs Inf is pretty consistent across all computer languages (and standard math definitions, too) . 0/0 is one example of NaN, and 1/0 is one example of Inf. – Carl Witthoft Mar 19 '13 at 11:20

2 Answers2

44

In short

NaN  : means 0/0 -- Stands for Not a Number
NA   : is generally interpreted as a missing, does not exist
NULL : is for empty object.

For an exact definition, you can read the documentation, which is very well written.

nbro
  • 15,395
  • 32
  • 113
  • 196
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • 2
    This answer contains at least two incorrect statements. Yes, `0/0` returns `NaN` but so does, e.g., `log(-1)`. Thus `NaN` does not mean `0/0`. It rather means "undefined" (in the mathematical sense). `NA` does not mean "does not exist". It rather means "unknown". – Roland Mar 15 '21 at 06:49
  • @Roland , please feel free to fix it as you like. I wrote this in a fast way. – agstudy Mar 20 '21 at 04:55
29

In R language, there are two closely related null-like values: NA and NULL. Both are used to represent missing or undefined values.

NULL represents the null object, it's a reserved word. NULL is perhaps returned by expressions and functions, so that values are undefined.

NA is a logical constant of length 1, which contains a missing value indicator. NA can be freely coerced to any other vector type except raw. There are also constants NA_integer_, NA_real_, NA_complex_ and NA_character_ of the other atomic vector types which support missing values: all of these are reserved words in the R language.

nbro
  • 15,395
  • 32
  • 113
  • 196
Mumtaz Ahmad
  • 422
  • 5
  • 12