gpt4 book ai didi

r - 使用“…”和“复制”

转载 作者:行者123 更新时间:2023-12-03 12:51:43 25 4
gpt4 key购买 nike

sapplyreplicate的文档中,有关使用...的警告

现在,我可以这样接受,但想了解其背后的原因。因此,我创建了这个小巧的示例:

innerfunction<-function(x, extrapar1=0, extrapar2=extrapar1)
{
cat("x:", x, ", xp1:", extrapar1, ", xp2:", extrapar2, "\n")
}

middlefunction<-function(x,...)
{
innerfunction(x,...)
}

outerfunction<-function(x, ...)
{
cat("Run middle function:\n")
replicate(2, middlefunction(x,...))
cat("Run inner function:\n")
replicate(2, innerfunction(x,...))
}

outerfunction(1,2,3)
outerfunction(1,extrapar1=2,3)
outerfunction(1,extrapar1=2,extrapar2=3)


也许我做了一些明显的可怕错误,但是我发现这种不愉快的结果。因此,任何人都可以向我解释为什么在上述所有对 outerfunction的调用中,我得到以下输出:

Run middle function:
x: 1 , xp1: 0 , xp2: 0
x: 1 , xp1: 0 , xp2: 0
Run inner function:
x: 1 , xp1: 0 , xp2: 0
x: 1 , xp1: 0 , xp2: 0


就像我说的那样:文档似乎对此有所警告,但我不明白为什么会这样。

最佳答案

在“示例”部分中的?replicate明确告诉我们,您尝试执行的操作无效且无效。在Note?replicate部分中,我们有:

     If ‘expr’ is a function call, be aware of assumptions about where
it is evaluated, and in particular what ‘...’ might refer to. You
can pass additional named arguments to a function call as
additional named arguments to ‘replicate’: see ‘Examples’.


如果我们查看示例,则会看到:

 ## use of replicate() with parameters:
foo <- function(x=1, y=2) c(x,y)
# does not work: bar <- function(n, ...) replicate(n, foo(...))
bar <- function(n, x) replicate(n, foo(x=x))
bar(5, x=3)


我对这些文档的理解是,它们的作用远不只是警告您在 ...调用中使用 replicate()。他们明确证明它不起作用。该帮助文件中的许多讨论都与其他功能的 ...参数有关,而不一定与 replicate()有关。

关于r - 使用“…”和“复制”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6704536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com