R功能;一个无关紧要的论点;怎么处理?

时间:2017-03-09 07:04:47

标签: r

如果我写一个简单的r函数

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

并使用&#34; silly_option&#34;来调用它。函数定义中不存在的参数:

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

然后我收到错误,正如所料:

unused argument (silly_option = 6)

另一方面,为什么这个调用没有返回类似的错误?

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

意味着()默默地忽略它或用它做某事吗?

1 个答案:

答案 0 :(得分:2)

从R控制台键入getAnywhere("mean.default")会显示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
}

mean.default()包中的base函数有一个包含varargs的签名,由省略号...表示为签名中的最后一个参数。

所以,我认为mean()默默地忽略了你的额外参数。

阅读here以获取有关R省略号功能的更多信息。