[R] Effeciently sum 3d table

David Winsemius dwinsemius at comcast.net
Mon Apr 16 22:54:53 CEST 2012


On Apr 16, 2012, at 4:04 PM, David A Vavra wrote:

>> even now you _could_ be clearer
>
> I fail to see why it's unclear.
>
>>> I'm after T1 + T2 + T3 + ...
>> Which would be one number ... i.e. the result you originally said you
>> did not want.
>
> I think it's precisely what I want. If I have two 3d tables, T1 and  
> T2, then
> say either
> 	1) T1 + T2
> 	2) T1 - T2
> (1) yields a third table equal to the sum of the individual cells  
> and (2)
> yields a table full of zeroes. At least it does for matrices. Are  
> you saying
> the T1+T2+T3+... above is equivalent to:
>
>   sum(T1)+sum(T2)+sum(T3)+....
>
> when the table has more than 2d? I tried it out by hand I get the  
> result I'm
> after.

For me (with my slightly constricted mindset) it would have been  
clearer to have started out talking about matrices and arrays.  An  
example would have save a bunch of time.

> What I want is a general solution. Reduce may be the answer but I
> find the documentation for it a bit daunting. Not to mention that it  
> is far
> from obvious that I should have originally thought of using it.

It is a function designed to do exactly what you requested: "Reduce  
uses a binary function to successively combine the elements of a given  
vector". As it turns out the term 'vector' in this case includes lists  
of classed and/or dimensioned objects rather than being restricted to  
atomic vectors.

-- 
David.

>
> DAV
>
>
>
> -----Original Message-----
> From: David Winsemius [mailto:dwinsemius at comcast.net]
> Sent: Monday, April 16, 2012 3:26 PM
> To: David A Vavra
> Cc: 'Petr Savicky'; r-help at r-project.org
> Subject: Re: [R] Effeciently sum 3d table
>
>
> On Apr 16, 2012, at 2:43 PM, David A Vavra wrote:
>
>> Thanks Petr,
>>
>> I'm after T1 + T2 + T3 + ...
>
> Which would be one number ... i.e. the result you originally said you
> did not want.
>
>> and your solution is giving a list of n items
>> each containing sum(T[i]). I guess I should have been clearer in
>> stating
>> what I need.
>
> Or even now you _could_ be clearer. Do you want successive partial
> sums? That would yield to:
>
> Reduce("+", listoftables, accumaulate=TRUE)
>
>
>
>
>>
>> Cheers,
>> DAV	
>>
>>
>>
>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org
>> ] On
>> Behalf Of Petr Savicky
>> Sent: Monday, April 16, 2012 11:07 AM
>> To: r-help at r-project.org
>> Subject: Re: [R] Effeciently sum 3d table
>>
>> On Mon, Apr 16, 2012 at 10:28:43AM -0400, David A Vavra 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.
>>
>> Hi.
>>
>> Use lapply(), for example
>>
>> listoftables <- list(array(1:8, dim=c(2, 2, 2)), array(2:9,
>> dim=c(2, 2,
>> 2)))
>> lapply(listoftables, sum)
>>
>> [[1]]
>> [1] 36
>>
>> [[2]]
>> [1] 44
>>
>> Hope this helps.
>>
>> Petr Savicky.
>>
>> ______________________________________________
>> 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.
>>
>> ______________________________________________
>> 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
>

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list