省略号-传入或传出其他方法的其他参数

sca*_*ace 2 r ellipsis

我了解点对点通常是什么意思。我想在要使用未知数量的参数创建自己的函数时了解如何使用它。

我不了解它如何工作,例如在function上variable.names()。当我执行时?variable.names,将编写以下内容:

...传入或传出其他方法的其他参数。

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

Len*_*ski 5

省略号参数允许将参数传递给下游函数。我们将通过以下简单的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)
Run Code Online (Sandbox Code Playgroud)

...以及输出:

> 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
Run Code Online (Sandbox Code Playgroud)