How to pass arguments to function called from mapply directly OR how to treat vector as argument to function not to mapply

两盒软妹~` 提交于 2019-12-11 04:42:51

问题


Suppose I have following function:

my.fun1 <- function(a,b,c){
  a * c + b
}

If I want to call it several times with multiple arguments I can do:

> my.fun1(1, 1, c(1:3))
[1] 2 3 4
> my.fun1(2, 2, c(1:3))
[1] 4 6 8
> my.fun1(3, 3, c(1:3))
[1]  6  9 12

But if I use mapply I get this:

> mapply(my.fun1, c(1:3), c(1:3), c(1:3))
[1]  2  6 12

Instead of desired:

[[1]]
[1] 2 3 4

[[2]]
[1] 4 6 8

[[3]]
[1]  6  9 12

IMHO the problem is that mapply basically translates the function call to:

> my.fun1(1, 1, 1)
[1] 2
> 
> my.fun1(2, 2, 2)
[1] 6
> 
> my.fun1(3, 3, 3)
[1] 12

How can I pass last argument of mapply directly to my.fun1 without being treat as argument to mapply but rather to my.func1?

PS: I've been playing with anonymous functions inside maplpy call. The closest is get (based on suggestions here):

> x <- mapply(function(x, y){my.fun1(x, y, c(1:3))}, c(1:3), c(1:3))
> split(x, rep(1:ncol(x), each = nrow(x)))
$`1`
[1] 2 3 4

$`2`
[1] 4 6 8

$`3`
[1]  6  9 12

But I guess this is ugly approach and there must be better way.


回答1:


As the last input in my.fun1 is the same vector, we place that in a list and pass it as argument for Map or mapply.

Map(my.fun1, 1:3, 1:3, list(1:3))

Or as @baptiste mentioned, the constants can be passed with MoreArgs

Map(my.fun1, 1:3, 1:3, MoreArgs = list(c=1:3))

When we are using mapply, it is better to have SIMPLIFY=FALSE to avoid coercing the list to matrix (if the lengths of the list elements are the same.

mapply(my.fun1, 1:3, 1:3, list(1:3), SIMPLIFY=FALSE)


来源:https://stackoverflow.com/questions/36421647/how-to-pass-arguments-to-function-called-from-mapply-directly-or-how-to-treat-ve

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!