[R] Effeciently sum 3d table

David Winsemius dwinsemius at comcast.net
Tue Apr 17 00:01:37 CEST 2012


On Apr 16, 2012, at 5:41 PM, Greg Snow wrote:

> Here is a simple example:
>
>> mylist <- replicate(4, matrix(rnorm(12), ncol=3), simplify=FALSE)
>> A <- Reduce( `+`, mylist )
>> B <- mylist[[1]] + mylist[[2]] + mylist[[3]] + mylist[[4]]
>> all.equal(A,B)
> [1] TRUE
>
> Basically what Reduce does is it first applies the function (`+` in
> this case) to the 1st 2 elements of mylist, then applies it to that
> result and the 3rd element, then that result and the 4th element (and
> would continue on if mylist had more than 4 elements).  It is
> basically a way to create functions like sum from functions like `+`
> which only work on 2 objects at a time.
>
> Another way to see what it is doing is to run something like:
>
>> Reduce( function(a,b){ cat("I am adding",a,"and",b,"\n"); a+b },  
>> 1:10 )
>
> The Reduce function will probably not be any faster than a really well
> written loop, but will probably be faster (both to write the command
> and to run) than a poorly designed naive loop application.
>

It's faster on my machine (but only fractionally) but it has the as  
yet unremarked-upon advantage that it will preserve attributes of the  
tables such as dimnames.

 > system.time(ans1 <- array(do.call(mapply,c(sum,z)),dim=2:4))
    user  system elapsed
   0.841   0.007   0.851
 > system.time(ans2 <-array(rowSums(do.call(cbind,z)),dim=2:4))
    user  system elapsed
   0.132   0.003   0.145

And on my system ....  the Reduce strategy wins by a hair:

 > system.time(ans3 <- Reduce("+", z) )
    user  system elapsed
   0.129   0.001   0.134

-- 
(the other) David.
>
> On Mon, Apr 16, 2012 at 12:52 PM, David A Vavra  
> <davavra at verizon.net> wrote:
>> Thanks Greg,
>>
>> I think this may be what I'm after but the documentation for it isn't
>> particularly clear. I hate it when someone documents a piece of  
>> code saying
>> it works kinda like some other code (running elsewhere, of course)  
>> making
>> the tacit assumption that everybody will immediately know what that  
>> means
>> and implies.
>>
>> I'm sure I'll understand it once I know what it is trying to  
>> say. :) There's
>> an item in the examples which may be exactly what I'm after.
>>
>> DAV
>>
>>
>> -----Original Message-----
>> From: Greg Snow [mailto:538280 at gmail.com]
>> Sent: Monday, April 16, 2012 11:54 AM
>> To: David A Vavra
>> Cc: r-help at r-project.org
>> Subject: Re: [R] Effeciently sum 3d table
>>
>> Look at the Reduce function.
>>
>> On Mon, Apr 16, 2012 at 8:28 AM, David A Vavra  
>> <davavra at verizon.net> wrote:
>>> I have a large number of 3d tables that I wish to sum
>>> Is there an efficient way to do this? Or perhaps a function I can  
>>> call?
>>>
>>> I tried using do.call("sum",listoftables) but that returns a  
>>> single value.
>>>
>>> So far, it seems only a loop will do the job.
>>>
>>>
>>> TIA,
>>> DAV
>>
>>
>> --
>> Gregory (Greg) L. Snow Ph.D.
>> 538280 at gmail.com
>>
>
>
>
> -- 
> Gregory (Greg) L. Snow Ph.D.
> 538280 at gmail.com
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list