Using the following block of code:
x<-1
mapply(sum, list(x<-x+1))
x
mapply(sum, MoreArgs = list(x<-x+1))
x
I get the following output:
> mapply(sum, list(x<-x+1))
[1] 2
> x
[1] 2
> mapply(sum, MoreArgs = list(x<-x+1))
list()
> x
[1] 3
Why do the results from the two uses of mapply
differ? I was under the strong impression that I would get the same result.
The number of times mapply
iterates depend on the length of the ...
arguments. In the second call you don't provide any argument and so mapply
iterates 0 times. It doesn't matter what you have passed to MoreArgs
.
Some examples:
mapply(match,integer(0),integer(0))
#list()
mapply(match,integer(0),integer(0), MoreArgs=list(table=1:10))
#list()
mapply(match,integer(0),1:10, MoreArgs=list(nomatch=2))
#Error in mapply(match, integer(0), 1:10, MoreArgs = list(nomatch = 2)) :
# zero-length inputs cannot be mixed with those of non-zero length
Everything is clearly documented in ?mapply
. In the arguments section:
...: arguments to vectorize over (vectors or lists of strictly positive length, or all of zero length). See also ‘Details’.
In Details:
‘mapply’ calls ‘FUN’ for the values of ‘...’ (re-cycled to the length of the longest, unless any have length zero), followed by the arguments given in ‘MoreArgs’.