[R] What is the difference between Reduce(…) and do.call(…) ?

Uwe Ligges ligges at statistik.tu-dortmund.de
Tue Oct 8 20:08:13 CEST 2013


Reduce will rbind 2 elements and then the result with the next element 
of the list --- while do.call just applies rbind once on all elements of 
the list at the same time.

Uwe Ligges




On 08.10.2013 11:22, Asis Hallab wrote:
> Dear R-Experts,
>
> using both do.call(…) and Reduce(…), I wonder about the differences of both.
> Please consider the following example:
>
> m <- matrix( 1:9, ncol=3 )
> lst <- list( m, 2*m, 3*m )
>
> rbind( lst )
> # Returns
>          [,1]      [,2]      [,3]
> tmp.lst Integer,9 Numeric,9 Numeric,9
>
> do.call( 'rbind', tmp.lst )
> # Returns
>        [,1] [,2] [,3]
>   [1,]    1    4    7
>   [2,]    2    5    8
>   [3,]    3    6    9
>   [4,]    2    8   14
>   [5,]    4   10   16
>   [6,]    6   12   18
>   [7,]    3   12   21
>   [8,]    6   15   24
>   [9,]    9   18   27
>
> Reduce( rbind, tmp.lst )
> # Returns the same
>        [,1] [,2] [,3]
>   [1,]    1    4    7
>   [2,]    2    5    8
>   [3,]    3    6    9
>   [4,]    2    8   14
>   [5,]    4   10   16
>   [6,]    6   12   18
>   [7,]    3   12   21
>   [8,]    6   15   24
>   [9,]    9   18   27
>
> So, what is the difference between Reduce and do.call and when best to
> use which?
>
> Your help will be much appreciated.
>
> Kind regards!
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list