[R] sum

Spencer Graves spencer.graves at pdf.com
Wed Apr 23 20:49:07 CEST 2003


Can you create a 3-d array rather than a list?  If yes, "apply" will work.

hth.  spencer graves

Jerome Asselin wrote:
> The only way I can see is to vectorize your list of matrices. Here's an 
> example.
> 
> matlist <- lapply(1:10,function(i) matrix(rnorm(12),3,4))
> summat <- matrix(sapply(matlist,I)%*%rep(1,10),3,4)
> 
> You can use the loop below to verify that the above solution is correct.
> 
> forsum <- matrix(0,3,4)
> for(i in 1:10)
> forsum <- forsum+matlist[[i]]
> 
> "summat" and "forsum" should be the same.
> 
> HTH,
> Jerome
> 
> On April 23, 2003 10:57 am, 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...
> 
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help



More information about the R-help mailing list