区别 mapply 和 .mapply

Difference mapply and .mapply

我试图找到有关 .mapply 的信息,但没有找到任何好的解释。那么谁能解释一下 mapply 和 .mapply 之间的区别?

示例: 为什么 .mapply(cbind,mylist,NULL) 有效但无效:

mapply(cbind,mylist,NULL)

?

    mylist=list(list(data.frame(a=3,b=2,c=4),data.frame(d=5,e=6,h=8),data.frame(k=2,e=3,b=5,m=5)),
                  list(data.frame(a=32,b=22,c=42),data.frame(d=5,e=63,h=82),data.frame(k=2,e=33,b=5,m=5)),
                  list(data.frame(a=33,b=21,k=41,c=41),data.frame(d=5,e=61,h=80),data.frame(k=22,e=3,b=5,m=5)))

?

来自 ?.mapply :

.mapply is ‘bare-bones’ versions for use in other R packages.

所以 .mapply 只是 mapply 的一个简单(较少参数)版本,可以在您自己的包中使用。实际上 mapply 内部调用 .mapply 然后做一些结果简化。

mapply <- 
function (FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES = TRUE) 
{
    FUN <- match.fun(FUN)
    dots <- list(...)
    answer <- .mapply(FUN, dots, MoreArgs) 
    ## ...
    ## the rest of the function is to simplify the result 

}

OP 编辑​​后更新

mapply(cbind,mylist,NULL)

不起作用,因为这里的 NULL 被视为点参数而不是 MoreArgs 参数。实际上,您使用 .mapply 重现了相同的错误:

 .mapply(cbind,list(mylist,NULL),NULL)

如果您显式地写参数名称,则可以在 mapply 中避免此错误;

 mapply(cbind,mylist,MorgeArgs=NULL)

但是由于 mapply 中的一行:

dots <- list(...)

您不会得到与 .mapply

相同的结果

最后,如果您只想 unlist 嵌套列表,最好在这里使用类似的东西:

lapply(mylist,unlist)   # faster and you you get the same output as .mapply