[R] sum of list elements

Patrick Burns pburns at pburns.seanet.com
Fri Jul 3 10:50:26 CEST 2009


My guess is that the original
poster is expecting a matrix
not a scalar.  If so, then:

 > Reduce('+', a)
      [,1] [,2] [,3]
[1,]    3    0    0
[2,]    0    3    0
[3,]    0    0    3



Patrick Burns
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of "The R Inferno" and "A Guide for the Unwilling S User")


David Winsemius wrote:
> 
> 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
> 
> ______________________________________________
> 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.
>




More information about the R-help mailing list