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

Tamas K Papp tpapp at Princeton.EDU
Fri Aug 19 10:37:56 CEST 2005


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,

Tamas




More information about the R-help mailing list