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

Asis Hallab asis.hallab at gmail.com
Tue Oct 8 11:22:53 CEST 2013


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!



More information about the R-help mailing list