Re: [R] 'mean' function

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Sat 01 Oct 2005 - 06:58:26 EST

On 9/30/2005 5:57 PM, Lisa Wang wrote:
> Hello,
>
> Could you please let me know how to see the "mean" function in R . The
> following is what I see when type in "mean" and "enter"
>
> mean
> function (x, ...)
> UseMethod("mean")
> <environment: namespace:base>
>
> I would like to see how the function is writen.

The "UseMethod" part tells you that mean is a generic function. There are lots of different implementations, depending on what sort of thing x is.

You can see the default one using "mean.default":

 > mean.default
function (x, trim = 0, na.rm = FALSE, ...) {

     if (!is.numeric(x) && !is.complex(x) && !is.logical(x)) {
         warning("argument is not numeric or logical: returning NA")
         return(as.numeric(NA))
     }
     if (na.rm)
         x <- x[!is.na(x)]
     trim <- trim[1]
     n <- length(x)
     if (trim > 0 && n > 0) {
         if (is.complex(x))
             stop("trimmed means are not defined for complex data")
         if (trim >= 0.5)
             return(median(x, na.rm = FALSE))
         lo <- floor(n * trim) + 1
         hi <- n + 1 - lo
         x <- sort(x, partial = unique(c(lo, hi)))[lo:hi]
         n <- hi - lo + 1
     }
     if (is.integer(x))
         sum(as.numeric(x))/n
     else sum(x)/n

}
<environment: namespace:base>

Duncan Murdoch



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat Oct 01 07:10:38 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 18:07:18 EST