Mean and var inconsistency (PR#2094)

Kevin.Wright@pioneer.com Kevin.Wright@pioneer.com
Wed, 2 Oct 2002 16:19:29 +0200 (MET DST)


Full_Name: Kevin Wright
Version: 1.5.1
OS: Windows 95
Submission from: (NULL) (170.54.59.160)


Originally posted to r-devel, but after thinking about it more decided that this
is a bug.  I encountered an unexpected difference in the way that mean and var
work: 

> mean(numeric(0)) 
[1] NaN 

> var(numeric(0)) 
Error in var(numeric(0)) : `x' is empty 

Looking into this, I discovered in mean.default that sum(x)/n 
evaluates as 
> sum(numeric(0))/length(numeric(0)) 
[1] NaN 

The function var seems to evaluate this: 
> .Internal(cov(numeric(0),NULL,"all.obs")) 
Error: `x' is empty 

One possible fix would be to modify var to check that the length of x is greater
than 0, otherwise return NaN/NA.  

Just for your information (not necessarily the desirable answer), in S-Plus
2000:
> mean(numeric(0))
NA
> var(numeric(0))
NA



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._