[R] why is sd(numeric(1))==NA (and not NaN)?

From: Tamas K Papp <tpapp_at_princeton.edu>
Date: Fri 19 Aug 2005 - 18:37:56 EST

I am just curious about this, and could not find anything in the help pages or the list archives. ?var mentions that for a vector of length 1 it gives NA instead of NaN as S-Plus does.

mean(numeric(0))==NaN, this makes sense since it is 0/0.

sd(numeric(1))==NA, but it is sqrt(0/0), so it makes more sense to me as NaN.

Perhaps sd(numeric(0)) (and var) could return NA then...

Please understand that I am not proposing a change to R (which is a very well-polished language) but would like to know the reasons for the above.

The issue came up when I was evaluating MCMC simulations. I needed to know the conditional mean and standard deviation of some variable given that it is nonzero, so I removed the zeroes and used mean() and sd() on it, not thinking about the case when all draws are zero. When that happened, mean() gave NaN gracefully, but sd() (of course, in compliance with ?var) failed. I don't see why NA would be so bad in that case, if somebody could please explain as there must be a reason.

Thank you,


R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Aug 19 18:44:53 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 15:33:18 EST