Re: [Rd] Benefit of treating NA and NaN differently for numerics

From: Ted Harding <Ted.Harding_at_manchester.ac.uk>
Date: Thu, 31 Dec 2009 21:05:09 +0000 (GMT)


On 31-Dec-09 20:43:43, Saptarshi Guha wrote:
> Hello,
> I notice in main/arithmetic.c, that NA and NaN are encoded
> differently(since every numeric NA comes from R_NaReal which is
> defined via ValueOfNA)
> What is the benefit of treating these two differently? Why can't NA
> be a synonym for NaN?
>

> Thank you
> Saptarshi
> (R-2.9)

Because they are used to represent different things. Others will be able to give you a much more comprehensive account than I can of their uses in R, but essentially:

NaN represents a result which is not valid (i.e. "Not a Number") in the domain of quantities being evaluated. For example, R does its arithmetic by default in the domain of "double", i.e. the machine representation of real numbers. In this domain, sqrt(-1) does not exist -- it is not a number in the domain of real numbers. Hence:

  sqrt(-1)

  # [1] NaN
  # Warning message:
  # In sqrt(-1) : NaNs produced

In order to obtain a result which does exist, you need to switch domain to complex numbers:

  > sqrt(as.complex(-1))
  # [1] 0+1i

NA, on the other hand, represents a value (in whatever domain: double, logical, character, ...) which is not known, which is why it is typically used to represent missing data. It would be a valid entity in the current domain if its value were known, but the value is not known. Hence the result of any expression involving NA quantities is NA, since the value if the expression would depend on the unkown elements, and hence the value of the expression is unknown.

This distinction is important and useful, so it should not be done away with by merging NaN and NA!

Best wishes,
Ted.



E-Mail: (Ted Harding) <Ted.Harding_at_manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861
Date: 31-Dec-09                                       Time: 21:05:06
------------------------------ XFMail ------------------------------

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Thu 31 Dec 2009 - 21:10:05 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 31 Dec 2009 - 23:40:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive