[R] trimming observations

David Winsemius dwinsemius at comcast.net
Wed Aug 19 23:10:10 CEST 2009


On Aug 19, 2009, at 2:22 PM, Juliane Struve wrote:

> Dear list,
>
> the trim argument in mean(x,trim=0.1) can be used to exclude a  
> fraction of the observations when calculating the mean. There does  
> not seem to be a trim argument for sd(), is there some other way of  
> excluding observations when calculating the standard deviation ?

You could copy the methods used in mean. getAnywhere(mean.default)  
exposes the preprocessing that is done before the vector is submitted  
to .Internal(mean(x))

if (trim > 0 && n) {
         if (is.complex(x))
             stop("trimmed means are not defined for complex data")
         if (trim >= 0.5)
             return(stats::median(x, na.rm = FALSE))
         lo <- floor(n * trim) + 1
         hi <- n + 1 - lo
         x <- sort.int(x, partial = unique(c(lo, hi)))[lo:hi]

-- 
David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list