[R] possible bug in sd

Claudio Agostinelli mail-list at linaria.dst.unive.it
Mon Oct 21 11:48:02 CEST 2002


Dear All,
I think there is a small bug in sd when the argument is a dataframe and
there are missing values:

> x <- data.frame(matrix(rnorm(12,0,1), nrow=4, ncol=3))
> x[1,1] <- NA
> sd(x)
Error in var(x, na.rm = na.rm) : missing observations in cov/cor
> sd(x, na.rm=TRUE)
Error in var(x, na.rm = na.rm) : missing observations in cov/cor
> sapply(x, sd, na.rm=TRUE)
       X1        X2        X3
1.0308198 0.4817945 1.5692881
> version
         _
platform i686-pc-linux-gnu
arch     i686
os       linux-gnu
system   i686, linux-gnu
status
major    1
minor    6.0
year     2002
month    10
day      01
language R

The way to fix it should be to replace the lines:
        sapply(x, sd)
in the definition of the sd function with
        sapply(x, sd, na.rm=na.rm)

Best,
Claudio Agostinelli

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help 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-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list