DATA MINING
Desktop Survival Guide by Graham Williams |
|||||
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> |
> 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> |