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.