[R] Effeciently sum 3d table

Petr Savicky savicky at cs.cas.cz
Mon Apr 16 17:06:49 CEST 2012


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.



More information about the R-help mailing list