Dear All, Is there an automatic way to sum the elements of a list? For example given a = list(diag(3), diag(3), diag(3)) to obtain b=a[1]+a[2]+a[3]. Thanks Ivo
I think you are asking for Reduce > Reduce(`+`, a) [,1] [,2] [,3] [1,] 3 0 0 [2,] 0 3 0 [3,] 0 0 3 > You might be asking for do.call with some function's name as its first argument. > do.call("sum", a) [1] 9 >
On Jul 2, 2009, at 10:20 PM, Ivo Shterev wrote:> a = list(diag(3), diag(3), diag(3))a = list(diag(3), diag(3), diag(3)) > sum(unlist(a)) [1] 9 David Winsemius, MD Heritage Laboratories West Hartford, CT
On Jul 2, 2009, at 11:24 PM, David Winsemius wrote:> > On Jul 2, 2009, at 10:20 PM, Ivo Shterev wrote: > >> a = list(diag(3), diag(3), diag(3)) > > > > a = list(diag(3), diag(3), diag(3)) > > sum(unlist(a)) > [1] 9> Also:> sum(sapply(a,I)) [1] 9 David Winsemius, MD Heritage Laboratories West Hartford, CT