什么时候不能用R的向量回收规则代替mapply的MoreArgs参数? [英] When can mapply's MoreArgs argument not be replaced by R's vector recycling rules?

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

问题描述

我想举一个例子,说明 mapply MoreArgs 参数何时有用.我被彻底击败了.令人发指的是,即使 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 提供相同的功能.这使我回到原来的问题:什么时候可以使用 mapply MoreArgs 参数提供无法从 ... 参数和向量回收规则?我还没有看到仅通过删除 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 ....

推荐答案

我还没有看到仅通过删除 MoreArgs = 参数并将相关部分传递给 ... 即可获得相同功能的情况./p>

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),这使我有信心调用相同的行为.第四种情况仅是显示我们很傻而根本不使用任何列表时得到的结果.

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的向量回收规则代替(尽管我的

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆