[R] possible bug in sd

Liaw, Andy andy_liaw at merck.com
Mon Oct 21 14:29:28 CEST 2002


> From: Claudio Agostinelli [mailto:mail-list at linaria.dst.unive.it]
> 
> 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

That's because sd looks like the following:

function (x, na.rm = FALSE) 
{
    if (is.matrix(x)) 
        apply(x, 2, sd)
    else if (is.vector(x)) 
        sqrt(var(x, na.rm = na.rm))
    else if (is.data.frame(x)) 
        sapply(x, sd)
    else sqrt(var(as.vector(x), na.rm = na.rm))
}

Notice how the na.rm argument never got passed in the apply() and sapply()
calls.  I'd call that a bug alright.

Cheers,
Andy

------------------------------------------------------------------------------
Notice: This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (Whitehouse Station, New Jersey, USA) that may be confidential, proprietary copyrighted and/or legally privileged, and is intended solely for the use of the individual or entity named on this message. If you are not the intended recipient, and have received this message in error, please immediately return this by e-mail and then delete it.

==============================================================================

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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