[R] Still a bug with NA in sd() or var()?

Roger Dungan roger.dungan at canterbury.ac.nz
Mon Oct 31 22:23:43 CET 2005


Dear R-users,

Running R 2.1.1 in WindowsXP, there seems to be a 'bug' in sd()
If
>x<-c(1,2,3,NA,5)
>mean(x) 
[1] NA

But 
>sd(x) 
Or
>var(x)
give
Error in var(x, na.rm = na.rm) : missing observations in cov/cor

There are obvious work-rounds, like
>sd(x, is.na(x)==F)
which gives the result (with error message)
[1] 1.707825
Warning message:
the condition has length > 1 and only the first element will be used in:
if (na.rm) "complete.obs" else "all.obs"

or

>y<-subset(x, is.na(x)==F)
>sd(y)
[1] 1.707825

Am I missing something, or is this a problem with sd()? Why does mean(x)
give a simple NA, but var(x) requires some additional work? There was
some previous discussion in r-help about this for R v 1.6.0, with
mention of a fix for 1.6.1. 

Dr Roger Dungan
School of Biological Sciences
University of Cantebury
Christchurch, New Zealand
ph +64 3 366 7001 ext. 4848
fax +64 3 364 2590




More information about the R-help mailing list