curious_cat
curious_cat

Reputation: 837

R functions; a call extraneous arguments; how is it handled?

If I write a simple r function

foofunction<-function(x,y)
{
...
}

and call it like so with a "silly_option" argument that does not exist in the function definition:

foofunction(x=5,y=10,silly_option=6)

Then I get an error, as expected:

unused argument (silly_option = 6)

On the other hand, why doesn't this call return a similar error?

mean(c(1,2,3),silly_option=6)

Is mean() silently ignoring it or doing something with it?

Upvotes: 0

Views: 85

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521194

Typing getAnywhere("mean.default") from the R console reveals the source code for mean():

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(NA_real_)
    }
    # ... more code
}

The mean.default() function in the base package has a signature which includes varargs, indicated by the ellipsis ... as the last parameter in the signature.

So, I think that mean() is silently ignoring your extra parameter.

Read here for more information on R's ellipsis function capability.

Upvotes: 2

Related Questions