Asis Hallab
2013-Oct-08 09:22 UTC
[R] What is the difference between Reduce(…) and do.call(…) ?
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!
Barry Rowlingson
2013-Oct-08 09:46 UTC
[R] What is the difference between Reduce(…) and do.call(…) ?
On Tue, Oct 8, 2013 at 10:22 AM, Asis Hallab <asis.hallab at gmail.com> wrote:> Dear R-Experts,> So, what is the difference between Reduce and do.call and when best to > use which?>From the help:?Reduce? uses a binary function to successively combine the elements of a given vector and a possibly given initial value. ?do.call? constructs and executes a function call from a name or a function and a list of arguments to be passed to it. In your example, rbind gets called N-1 times when using Reduce (where N is the length of the list) because it is doing something like: rbind(x[[1]],rbind(x[[2]],x[[3]])) For longer lists, its doing something like (where a b c d e are list elements x[[1]] to x[[5]]) rbind( a, rbind( b, rbind( c, rbind( d, e ) ) ) ) whereas do.call calls it once, as rbind(a,b,c,d,e) Use Reduce when you are doing a Reduce operation (read up on Functional Programming). Use do.call when you want to call a function with parameters in a list. Barry
Uwe Ligges
2013-Oct-08 18:08 UTC
[R] What is the difference between Reduce(…) and do.call(…) ?
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. >