Mean and var inconsistency (PR#2094)
Wed, 2 Oct 2002 16:19:29 +0200 (MET DST)

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

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

> 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
> mean(numeric(0))
> var(numeric(0))

r-devel mailing list -- Read
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: