我理解点-点-点的一般含义。当我想创建自己的参数数量未知的函数时,我了解如何使用它。
我不明白它是如何工作的,例如函数 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
关于R 省略号 - 传递给其他方法或从其他方法传递的进一步参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48480567/