Вопрос
I have a function in R that takes in 3 parameters, say foo(x,y,z)
.
When I call the function, I really have a list of elements for x
, and a list for y
but only one element for z
. If Z
was a list, and I wanted to apply foo
to each element, mapply(foo, x, y, z)
works.
However, since z
is not a list, mapply(foo,x,y,z)
does not work.
More specifically, if x
and y are lists of 3 elements each, the following does work however: mapply(foo, x, y, list(z, z, z))
.
Is there a way I can perhaps combine mapply
and sapply
without me first making z
into a list of 3 elements? I want z
to just be reused!
Edit 1: I was asked for an example:
mylist1 <- list(c(5,4), c(7,9), c(8,3))
mylist2<- list(c(2,3), c(6,7), c(10,11))
item3 <- matrix(data = 15, nrow = 3, ncol = 3)
foo <- function(x,y,z){
return(x[1]+y[2]+z[2,2])
}
The following works:
> mapply(foo, mylist1, mylist2, list(item3,item3, item3))
[1] 23 29 34
The following does not work:
mapply(foo, mylist1, mylist2, item3)
Error in z[2, 2] : incorrect number of dimensions
Решение
Use the MoreArgs
argument to mapply
mapply(foo, x = mylist1, y= mylist2, MoreArgs = list(z = item3))
## [1] 23 29 34
Другие советы
You just have to put the last item in a list, and R will recycle it just fine:
mapply(foo, mylist1, mylist2, list(item3))
Note that the documentation specifically says that the arguments you pass need to be:
arguments to vectorize over (vectors or lists of strictly positive length, or all of zero length)
and you were trying to pass a matrix.