Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference mapply and .mapply

Tags:

r

apply

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

?
like image 582
user1665355 Avatar asked Mar 07 '15 23:03

user1665355


People also ask

What is difference between Sapply and Lapply?

If the programmers want the output to be a data frame or a vector, then sapply function is used whereas if a programmer wants the output to be a list then lapply is used. There is one more function known as vapply which is preferred over sapply, as vapply allows the programmer to specify the output type.

What is the difference between Lapply () and Sapply () function in R?

The lapply() function in R can be used to apply a function to each element of a list, vector, or data frame and obtain a list as a result. The sapply() function can also be used to apply a function to each element of a list, vector, or data frame but it returns a vector as a result.

What is Mapply?

mapply is a multivariate version of sapply . mapply applies FUN to the first elements of each ... argument, the second elements, the third elements, and so on. Arguments are recycled if necessary.

How does Mapply work in R?

mapply function in R The mapply() function is a multivariate apply of sorts which applies a function in parallel over a set of arguments. lapply()iterate over a single R object but What if you want to iterate over multiple R objects in parallel then mapply() is the function for you.


1 Answers

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
like image 147
agstudy Avatar answered Sep 30 '22 15:09

agstudy