user1665355
user1665355

Reputation: 3393

Difference mapply and .mapply

I was trying to find out info about .mapply but did not find any good explanation. So could anyone explain the difference between mapply and .mapply?

Example: Why does .mapply(cbind,mylist,NULL) works but not:

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)))

?

Upvotes: 5

Views: 423

Answers (1)

agstudy
agstudy

Reputation: 121598

From ?.mapply :

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

So .mapply is just a simple (less parameters) version of mapply to use in your own package. Indeed mapply call internally .mapply and then do some result simplification.

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 

}

UPDATE after OP edit

mapply(cbind,mylist,NULL)

does not work because NULL here is considered as dots arguments and not the MoreArgs parameter. Indeed you reproduce the same error with .mapply using :

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

You can avoid this error in mapply if you explicitly write the argument name ;

 mapply(cbind,mylist,MorgeArgs=NULL)

But due to a the line in mapply :

dots <- list(...)

You will not get the same result as with .mapply

Finally, if you want just to unlist you nested list , better here to use something like :

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

Upvotes: 5

Related Questions