DATA MINING
Desktop Survival Guide by Graham Williams |
|||||
Methods |
Sometimes you may want to know how a function is implemented. R is
also an object oriented functional language and so what method is
called depends on the type of the argument. This can be illustrated
with the mean function. We can try to determine its
implementation:
> mean function (x, ...) UseMethod("mean") <environment: namespace:base> |
> methods(mean) [1] mean.data.frame mean.Date mean.default mean.difftime [5] mean.POSIXct mean.POSIXlt |
> 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> |
> mean.data.frame function (x, ...) sapply(x, mean, ...) <environment: namespace:base> |
Note that the function getAnywhere may also be useful in obtaining the definition of objects.
Copyright © Togaware Pty Ltd Support further development through the purchase of the PDF version of the book.