I’m learning R recently and confused by two function: lapplyand do.call. It seems that they’re just similar to map function in Lisp. But why are there two functions with such a different name? Why doesn’t R just use a function called map?
I’m learning R recently and confused by two function: lapply and do.call . It
Share
There is a function called
Mapthat may be similar to map in other languages:lapplyreturns a list of the same length as X, each element of which is the result of applying FUN to the corresponding element of X.do.callconstructs and executes a function call from a name or a function and a list of arguments to be passed to it.Mapapplies a function to the corresponding elements of given vectors…Mapis a simple wrapper tomapplywhich does not attempt to simplify the result, similar to Common Lisp’s mapcar (with arguments being recycled, however). Future versions may allow some control of the result type.Mapis a wrapper aroundmapplylapplyis a special case ofmapplyMapandlapplywill be similar in many cases.For example, here is
lapply:And the same using
Map:do.calltakes a function as input and splatters its other arguments to the function. It is widely used, for example, to assemble lists into simpler structures (often withrbindorcbind).For example: