[R] sum

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Wed Apr 23 20:42:03 CEST 2003



Luis Silva wrote:
> Dear helpers
> 
> I have a list where each element is a matrix (the list is 
> obtained with lapply). I want to sum those matrices. Is there a 
> function to do that? The sum function sums all the elements...
> --
> 

How about:

R> r = list(matrix(1:4,2,2),matrix(5:8,2,2),matrix(9:12,2,2))
R> expr = paste("r[[",seq(along = r),"]]", collapse="+")
R> eval(parse(text = expr))
      [,1] [,2]
[1,]   15   21
[2,]   18   24


Regards,
Sundar



More information about the R-help mailing list