1

In the following small sample of code (tested using R 3.1.2), I suspect the comparison for is.nan(NA) results in FALSE because NA, although considered a missing value, is still regarded as a number.

x <- c(0, Inf, -Inf, NaN, NA)
is.nan(x)

[1] FALSE FALSE FALSE  TRUE FALSE

However:

x <- c(0, Inf, -Inf, NaN, NA)
is.na(x)

[1] FALSE FALSE FALSE  TRUE  TRUE

is quite perplexing since I assumed that the comparison of is.na(NaN) would be FALSE given NA is considered to be a number, albeit missing, but NaN is regarded a non-numeric value. This result seems contradictory.

Edit: I read the responses at R: Dealing with TRUE, FALSE, NA and NaN and I was surprised to read the following response:

"NaN is numeric so you can't encounter it in a logical vector." and "NA stands for "Not available", and is not the same as the general NaN ("not a number"). NA is generally used for a default value for a number to stand in for missing data; NaN's are normally generated because a numerical issue (taking log of -1 or similar)." Would I be remiss if I modified my understanding of NaN values from one where I thought of them as completely non-numeric (as the name would suggest) to one where they actually are numeric in nature but not in a generally understood definition, e.g., calculating the square root of a negative number which results in an imaginary value? If so, that would explain why is.na(NaN) results in a TRUE result because NA (numeric by definition) is mathematically compatible with NaN (numeric in a semi-esoteric sense).

Any constructive advice would be appreciated.

Thanks.

Community
  • 1
  • 1
CaitlinG
  • 1,955
  • 3
  • 25
  • 35

0 Answers0