4
Dieferenmente de lapply
, do.call
applies a function across a list (which is also a data.frame
). Consider the loop for
down below:
set.seed(123)
for (i in 1:6) {
assign(paste('var', i, sep = '_'),
runif(30, 20, 100))
}
I can get do.call
to transform these vectors into a data.frame
:
data_1 <- do.call(
cbind.data.frame,
mget(ls(pattern = '*v'))
)
But, this does not make sense, because the function itself (be it cbind.data.frame
, sum
, etc.) would do the same without the need to apply do.call
. For example:
data_2 <- cbind.data.frame(mget(ls(pattern = '*v')))
Now for the sum:
do.call(sum, data_2)
[1] 10826.89
sum(data_2)
[1] 10826.89
I ask you:
- In what context the function
do.call
would become indispensable? Why?
I haven’t been able to stop to answer, but the key idea is to make several calls in a row
– Tomás Barcellos
A classic example is
do.call
andrbind
for a list of results. I remember some comparisons that showed that thedo.call
was faster.– Tomás Barcellos