为什么在 mapply 的 MoreArgs 参数中进行变量赋值与在其“...”参数中进行变量赋值会产生不同的结果?
Why does doing variable assignment inside mapply's MoreArgs argument give different results than doing it inside its "..." argument?
使用以下代码块:
x<-1
mapply(sum, list(x<-x+1))
x
mapply(sum, MoreArgs = list(x<-x+1))
x
我得到以下输出:
> mapply(sum, list(x<-x+1))
[1] 2
> x
[1] 2
> mapply(sum, MoreArgs = list(x<-x+1))
list()
> x
[1] 3
为什么 mapply
的两种使用结果不同?我在 下,我会得到相同的结果。
mapply
迭代次数取决于 ...
参数的长度。在第二次调用中,您没有提供任何参数,因此 mapply
迭代了 0 次。您传递给 MoreArgs
.
的内容并不重要
一些例子:
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
一切都清楚地记录在 ?mapply
中。在参数部分:
...: arguments to vectorize over (vectors or lists of strictly
positive length, or all of zero length). See also ‘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’.
使用以下代码块:
x<-1
mapply(sum, list(x<-x+1))
x
mapply(sum, MoreArgs = list(x<-x+1))
x
我得到以下输出:
> mapply(sum, list(x<-x+1))
[1] 2
> x
[1] 2
> mapply(sum, MoreArgs = list(x<-x+1))
list()
> x
[1] 3
为什么 mapply
的两种使用结果不同?我在
mapply
迭代次数取决于 ...
参数的长度。在第二次调用中,您没有提供任何参数,因此 mapply
迭代了 0 次。您传递给 MoreArgs
.
一些例子:
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
一切都清楚地记录在 ?mapply
中。在参数部分:
...: arguments to vectorize over (vectors or lists of strictly positive length, or all of zero length). See also ‘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’.