Skip to content
Prev 59261 / 398502 Next

changing (core) function argument defaults?

Yes, I think for all practical purposes it (usually?) is. Here's an example.
Suppose I wish to change the default "constant" argument of mad from 1.48 to
2. Then
function (x, center = median(x), constant = 2, na.rm = FALSE, 
    low = FALSE, high = FALSE) 
{
    if (na.rm) 
        x <- x[!is.na(x)]
    n <- length(x)
    constant * if ((low || high) && n%%2 == 0) {
        if (low && high) 
            stop("`low' and `high' can't be both TRUE")
        n2 <- n%/%2 + as.integer(high)
        sort(abs(x - center), partial = n2)[n2]
    }
    else median(abs(x - center))
}

If you now attach the workspace/environment containing this newly defined
mad function to the search list before the stats package (which contains the
original mad()) you have effectively changed the default argument without
changing the function.

I hope experts will let us know when this can't be done (perhaps with
.internal functions or non-exported functions in namespaces, though it isn't
clear to me that one couldn't manually export them and do this here, too).

Of course, all the usual warnings about masking existing functions apply.

Cheers,

-- Bert Gunter
Genentech Non-Clinical Statistics
South San Francisco, CA
 
"The business of the statistician is to catalyze the scientific learning
process."  - George E. P. Box