0

对于以下代码和输出。

ind <- function(x, ..., drop=T) { x[...,,drop=drop]}
indd <- function(x, ..., drop=T) { ind(x, ..., drop=drop)}
x=array(1:24,1:4)
ind(x,,1,)
##      [,1] [,2] [,3] [,4]
## [1,]    1    7   13   19
## [2,]    3    9   15   21
## [3,]    5   11   17   23
indd(x,,,1,)
## Error in ind(x, ..., drop = drop) : argument is missing, with no default.

为什么 ... 适用于函数“ind”但不适用于函数“indd”?

当我直接调试函数“ind”时(ind(x,,1,)),我得到了

match.call(expand.dots=FALSE)
## ind(x = x, ... = list(, 1, ))
substitute(list(...))
## list(, 1, )

但是,如果调试是由“indd”(indd(x,,1,))触发的,我得到了

match.call(expand.dots=FALSE)
## ind(x = x, ... = list(..1, 1, ..3), drop = drop)
substitute(list(...))
## list(, 1, )

我不明白为什么match.call在两种情况下给出不同的结果但substitute(list(...))给出相同的结果。

如何通过调用使函数“indd”与函数“ind”一样工作?

非常感谢!

4

1 回答 1

0

我仍然不明白为什么match.call在两种情况下给出不同的结果。

但我找到了修复函数“indd”的技巧。

indd <- function(x, ..., drop=T)
{ 
  car = as.character(match.call(expand.dots=FALSE)$...)
  eval(parse(text=paste("ind(x,", paste(car, ",", collapse=""), "drop=drop)")))
}
于 2013-06-07T20:58:29.003 回答