R 省略号 - 传递给其他方法或从其他方法传递的进一步参数

R ellipsis - further arguments passed to or from other methods

我明白点-点-点的大体意思。当我想创建自己的参数数量未知的函数时,我了解如何使用它。

我不明白它是如何工作的,例如函数 variable.names()。当我执行?variable.names的时候,里面写着:

... further arguments passed to or from other methods.

这到底是什么意思?我不知道我可以在那里传递什么。如何以及在何处使用这些传递的参数。

省略号参数允许将参数传递给下游函数。我们将用一个简单的 R 函数进行说明,如下所示。

testfunc <- function(aFunction,x,...) {
     aFunction(x,...)
}
aVector <- c(1,3,5,NA,7,9,11,32)

# returns NA because aVector contains NA values
testfunc(mean,aVector)

# use ellipsis in testfunc to pass na.rm=TRUE to mean()
testfunc(mean,aVector,na.rm=TRUE)

...输出:

> testfunc <- function(aFunction,x,...) {
+      aFunction(x,...)
+ }
> aVector <- c(1,3,5,NA,7,9,11,32)
> 
> # returns NA because aVector contains NA values
> testfunc(mean,aVector)
[1] NA
> # use ellipsis in testfunc to pass na.rm=TRUE to mean()
> testfunc(mean,aVector,na.rm=TRUE)
[1] 9.714286