[R] the way to look at all the codings of any functions

Petr Klasterecky klaster at karlin.mff.cuni.cz
Mon Jun 18 07:00:15 CEST 2007


Someone with more experience could certainly provide a more precise 
answer, but basically you came across something called generic functions 
in R. This concept allows mean() to be called on various data structures 
without extra care.

See
?UseMethod
methods(mean)
getAnywhere(mean.default)

For more details see the R language definition manual.

Petr


ebi napsal(a):
> Dear SIr,
> 
> 
> In case of looking at the codes of the fuction, "cov",
> 
> we find all the codings below.  But, incase of "mean", we don't find
> the contents.
> 
> Please show me the way to look at all the codings of any functions.
> 
> Best regards,
> Kei
> -----------------------
> 
>> cov
> function (x, y = NULL, use = "all.obs", method = c("pearson",
>     "kendall", "spearman"))
> {
>     na.method <- pmatch(use, c("all.obs", "complete.obs",
> "pairwise.complete.obs"))
>     method <- match.arg(method)
>     if (is.data.frame(y))
>         y <- as.matrix(y)
>     else stopifnot(is.atomic(y))
>     if (is.data.frame(x))
>         x <- as.matrix(x)
>     else {
>         stopifnot(is.atomic(x))
>         if (!is.matrix(x)) {
>             if (is.null(y))
>                 stop("supply both 'x' and 'y' or a matrix-like 'x'")
>             x <- as.vector(x)
>         }
>     }
>     if (method == "pearson")
>         .Internal(cov(x, y, na.method, method == "kendall"))
>     else if (na.method != 3) {
>         Rank <- function(u) if (is.matrix(u))
>             apply(u, 2, rank, na.last = "keep")
>         else rank(u, na.last = "keep")
>         if (na.method == 2) {
>             ok <- complete.cases(x, y)
>             x <- if (is.matrix(x))
>                 x[ok, ]
>             else x[ok]
>             if (!is.null(y))
>                 y <- if (is.matrix(y))
>                   y[ok, ]
>                 else y[ok]
>         }
>         x <- Rank(x)
>         if (!is.null(y))
>             y <- Rank(y)
>         .Internal(cov(x, y, na.method, method == "kendall"))
>     }
>     else stop("cannot handle 'pairwise.complete.obs'")
> }
> <environment: namespace:stats>
>> mean
> function (x, ...)
> UseMethod("mean")
> <environment: namespace:base>
> 
> ______________________________________________
> R-help at 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
> and provide commented, minimal, self-contained, reproducible code.
> 

-- 
Petr Klasterecky
Dept. of Probability and Statistics
Charles University in Prague
Czech Republic



More information about the R-help mailing list