[R] Effeciently sum 3d table

David A Vavra davavra at verizon.net
Mon Apr 16 20:35:13 CEST 2012


Thanks Gunter,

I mean what I think is the normal definition of 'sum' as in: 
   T1 + T2 + T3 + ...
It never occurred to me that there would be a question.

I have gotten the impression that a for loop is very inefficient. Whenever I
change them to lapply calls there is a noticeable improvement in run time
for whatever reason. The problem with lapply here is that I effectively need
a global table to hold the final sum. lapply also  wants to return a value.

You may be correct that in the long run, the loop is the best. There's a lot
of extraneous memory wastage holding all of the tables in a list as well as
the return 'values'.

As an alternate and given a pre-existing list of tables, I was thinking of
creating a temporary environment to hold the final result so it could be
passed globally to each lapply execution level but that seems clunky and
wasteful as well. 

Example in partial code:

Env <- CreatEnv() # my own function
Assign('final',T1-T1,envir=env)
L<-listOfTables

lapply(L,function(t) {
	final <- get('final',envir=env) + t
	assign('final',final,envir=env)
	NULL
})

But I was hoping for a more elegant and hopefully more efficient solution.
Greg's suggestion for using reduce seems in order but as yet I'm unfamiliar
with the function.

DAV



-----Original Message-----
From: Bert Gunter [mailto:gunter.berton at gene.com] 
Sent: Monday, April 16, 2012 12:42 PM
To: Greg Snow
Cc: David A Vavra; r-help at r-project.org
Subject: Re: [R] Effeciently sum 3d table

Define "sum" . Do you mean you want to get a single sum for each
array? -- get marginal sums for each array? -- get a single array in
which each value is the sum of all the individual values at the
position?

Due thought and consideration for those trying to help by formulating
your query carefully and concisely vastly increases the chance of
getting a useful answer. See the posting guide -- this is a skill that
needs to be learned and the guide is quite helpful. And I must
acknowledge that it is a skill that I also have not yet mastered.

Concerning your query, I would only note that the two responses from
Greg and Petr that you received are unlikely to be significantly
faster than just using loops, since both are still essentially looping
at the interpreted level. Whether either give you what you want, I do
not know.

-- Bert

On Mon, Apr 16, 2012 at 8:53 AM, Greg Snow <538280 at gmail.com> wrote:
> 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


-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biost
atistics/pdb-ncb-home.htm



More information about the R-help mailing list