Togaware DATA MINING
Desktop Survival Guide
by Graham Williams
Google

Methods

Sometimes you may want to know how a function is implemented. R is also an object oriented function and so what method is called depends on the type of the argument. This can be illustrated with the mean function. Try to determine its implementation:

> mean
function (x, ...)
UseMethod("mean")
<environment: namespace:base>

The UseMethod part indicates that mean is a generic function, possibly with many different implementations. A generic function usually has a default method:

> 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 (is.integer(x))
        sum(as.numeric(x))/n
    else sum(x)/n
}
<environment: namespace:base>



Copyright © 2004-2006 [email protected]
Support further development through the purchase of the PDF version of the book.
Brought to you by Togaware.