Thursday, August 23, 2012

difference between NA and NaN in R

We usually see NA and NaN in R. What's the difference between them?

Here a good post for that topic:

In summary here:
  • NaN ("Not a Number") means 0/0
  • NA ("Not Available") is generally interpreted as a missing value and has various forms - NA_integer_, NA_real_, etc. 
  • Therefore, NaN ≠ NA and there is a need for NaN and NA.
  • returns TRUE for both NA and NaN, however is.nan() return TRUE for NaN (0/0) and FALSE for NA.
  • As the elements of an atomic vector must be of the same type there are multiple types of NA values. There is one case where this is particularly important to the user. The default type of NA is logical, unless coerced to some other type, so the appearance of a missing value may trigger logical rather than numeric indexing. Numeric and logical calculations with NA generally return NA. In cases where the result of the operation would be the same for all possible values the NA could take, the operation may return this value. In particular, ‘FALSE & NA’ is FALSE, ‘TRUE | NA’ is TRUE. NA is not equal to any other value or to itself; testing for NA is done using However, an NA value will match another NA value in match. 
  • Numeric calculations whose result is undefined, such as ‘0/0’, produce the value NaN. This exists only in the double type and for real or imaginary components of the complex type. The function is.nan is provided to check specifically for NaN, also returns TRUE for NaN. Coercing NaN to logical or integer type gives an NA of the appropriate type, but coercion to character gives the string "NaN". NaN values are incomparable so tests of equality or collation involving NaN will result in NA. They are regarded as matching any NaN value (and no other value, not even NA) by match. The NA of character type is as from R 1.5.0 distinct from the string "NA". Programmers who need to specify an explicit string NA should use ‘as.character(NA)’ rather than "NA", or set elements to NA using<-. As from R 2.5.0 there are constants NA_integer_, NA_real_, NA_complex_ and NA_ character_ which will generate (in the parser) an NA value of the appropriate type, and will be used in deparsing when it is not otherwise possible to identify the type of an NA (and the control options ask for this to be done). There is no NA value for raw vectors.

1 comment:

  1. Really cool post, highly informative and professionally written and I am glad to be a visitor of this perfect blog, thank you for this rare info!

    R Programming Online Training|
    Hadoop Online Training
    Data Science Online Training|