本文介绍了什么时候 mapply 的 MoreArgs 参数不能被 R 的向量回收规则取代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图想出一个例子来说明 mapplyMoreArgs 参数何时有用.我已经被彻底打败了.令人愤慨的是,即使 mapply 的文档中给出的例子也是不够的.文档将 mapply(rep, times = 1:4, MoreArgs = list(x = 42)) 作为他们使用 MoreArgs 的唯一示例,但我发现R 的向量回收规则意味着 mapply(rep, times = 1:4, 42) 给出完全相同的输出.

I'm trying to come up with an example of when mapply's MoreArgs argument is useful. I have been utterly defeated. Outrageously, even the example given in mapply's documentation is inadequate. The docs give mapply(rep, times = 1:4, MoreArgs = list(x = 42)) as their only example of the use of MoreArgs, but I have found that R's vector recycling rules mean that mapply(rep, times = 1:4, 42) gives exactly the same output.

那么什么时候可以不通过删除相应的 MoreArgs = list(...) 包装器来替换 MoreArgs 呢?我试图自己想出一些例子,但每次都失败了.例如,mapply(rnorm,1:10,11:20,MoreArgs = list(5),SIMPLIFY = FALSE) 等同于 mapply(rnorm,1:10,11:20,5,SIMPLIFY = FALSE).

So when can MoreArgs not be replaced by just deleting the corresponding MoreArgs = list(...) wrapper? I tried to come up with some examples myself, but I failed every time. For example, mapply(rnorm,1:10,11:20,MoreArgs = list(5),SIMPLIFY = FALSE) is identical to mapply(rnorm,1:10,11:20,5,SIMPLIFY = FALSE).

按照评论的引导,我也尝试过个例子,我们需要为每次调用回收一个向量到正在调用 mapply 的函数.但是,似乎 mapply 在使用包含所述向量的列表之间的功能上没有区别 - 例如list(c(0.6,0.3,0.1)) - 作为 ... 参数或作为 MoreArgs 参数.在 MoreArgs 的情况下,列表的内容按预期回收,在 ... 的情况下,向量回收规则意味着列表的内容将被回收在每种情况下,为 MoreArgs 提供相同的功能.这让我回到最初的问题:什么时候可以使用 mapplyMoreArgs 参数来提供无法从 ... 参数和向量回收规则?我还没有看到通过简单地删除 MoreArgs= 参数并让相关部分传递给 ... 无法获得相同功能的情况.

Following the lead of the comments, I've also tried examples where we need to recycle a vector for each call to the function that mapply is being called on. However, it seems that mapply has no difference in functionality between using a list containing said vector - e.g. list(c(0.6,0.3,0.1)) - as a ... argument or as a MoreArgs argument. In the MoreArgs case, the content of the list is recycled as expected and in the ... case, the vector recycling rules mean that the content of the list will be recycled in each case, giving identical functionality to MoreArgs. This brings me back to the original question: When can the MoreArgs argument of mapply be used to give functionality that cannot be gotten from the ... argument and the vector recycling rules? I've yet to see a case where identical functionality cannot be gained by simply deleting the MoreArgs= argument and letting the relevant parts pass to ....

推荐答案

这是错误的,但只是轻微的.比较:

This is wrong, but only slightly. Compare:

options(max.print = 50)#Before running this, make sure that you know how to undo it.

> mapply(sum,1:5,MoreArgs=list(runif(10),runif(10000)))
[1] 5019.831 5020.831 5021.831 5022.831 5023.831

> mapply(sum,1:5,list(runif(10)),list(runif(10000)))
[1] 5069.321 5070.321 5071.321 5072.321 5073.321

> mapply(sum,1:5,list(runif(10),runif(10000)))
[1]    6.658275 4984.177882    8.658275 4986.177882   10.658275

> mapply(sum,1:5,runif(10),runif(10000))
 [1] 1.750417 3.286090 3.186474 5.310268 5.962829 1.343564 2.325567 3.928796 4.955376
[10] 5.507385 1.992290 3.454536 3.399763 5.242883 5.589296 1.637056 2.964259 3.839006
[19] 5.647123 5.883139 1.863512 2.827110 3.633137 5.174900 5.365155 2.022725 3.139846
[28] 3.830624 5.064546 5.697612 1.242803 3.456888 3.726114 5.271773 5.881724 1.533730
[37] 2.489976 3.509690 5.657166 5.400823 1.972689 2.858276 3.571505 5.582752 5.482381
[46] 1.956237 2.497409 3.864434 5.389969 5.965341
 [ reached getOption("max.print") -- omitted 9950 entries ]
Warning message:
In mapply(sum, 1:5, list(runif(10), runif(10000))) :
  longer argument not a multiple of length of shorter

在第一种情况下,MoreArgs 参数中列表的每个元素都会在每次调用时被回收.类似地,第二种情况是为每次调用回收 runif(10)runif(10000) ,给出我有信心称之为相同的行为.第 4 种情况仅用于展示如果我们愚蠢到根本不使用任何列表时会得到什么.

In the first case, every element of the list in the MoreArgs argument is recycled for each call. Similarly, the second case is recycling both runif(10) and runif(10000) for each call, giving behavior that I'm confident to call identical. The fourth case only exists to shows what we get if we're silly enough to not use any lists at all.

我上面引用的说法是第一种和第三种情况应该相同.情况显然不是这样.如果我们尝试在没有 MoreArgs 的情况下使用一个列表(而不是两个,就像我们的第二种情况那样),R 的正常向量回收规则将使我们重用 runif(10) 对于第一次、第三次和第五次调用,并使用 runif(10000) 进行第二次和第四次调用,并由于这种奇怪的行为给我们一个警告.

My above quoted claim is that the first and third cases should be identical. This is clearly not the case. If we try to use one list (rather than two, as our second case did) without MoreArgs, R's normal vector recycling rules will have us reuse the value of runif(10) for the first, third, and fifth calls, and use runif(10000) for the second and fourth, and give us a warning due to this odd behavior.

总而言之,MoreArgs 参数似乎总是可以被 R 的向量回收规则替换(尽管我的 以前的答案),但不是我在问题中所说的确切方式.事实似乎是 MoreArgs=list(foo,bar,etc) 等价于使用 list(foo), list(bar), 和 list(etc) 作为 ... 参数给 mapply.请注意,这与使用 list(foo,bar,etc) 作为 ... 参数不同.因此,最终:您不会总是通过省略 MoreArgs 参数获得相同的功能.

In conclusion, it still appears that the MoreArgs argument can always be replaced by R's vector recycling rules (despite my previous answer), but not in the exact way that I said in the question. The truth appears to be that MoreArgs=list(foo,bar,etc) is equivalent to using list(foo), list(bar), and list(etc) as ... arguments to mapply. Notice that this is not the same as using list(foo,bar,etc) as a ... argument. So, ultimately: You will not always get identical functionality from omitting the MoreArgs argument.

最后一个小细节:省略 MoreArgs 参数是无害的,但省略 ... 参数并使用 MoreArgs 代替 给出意外输出,通常是一个空列表.

As a final minor detail: Omitting the MoreArgs argument is harmless, but omitting the ... argument and using MoreArgs instead gives unexpected output, usually an empty list.

这篇关于什么时候 mapply 的 MoreArgs 参数不能被 R 的向量回收规则取代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 15:21