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... -- http://adsl.sapo.pt
Luis, I use this: # sum of list l l.sum <- l[[1]] # just to create the result matrix with proper dimensions and dimnames l.sum[] <- colSums(do.call("rbind", l)) There might (or might not) be a problem if your matrix is stored by row. Just a word caution as I never checked this out. Hope this helps, Vadim> -----Original Message----- > From: Luis Silva [mailto:lm.silva at sapo.pt] > Sent: Wednesday, April 23, 2003 10:58 AM > To: R help > Subject: [R] sum > > > 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... > -- > > > http://adsl.sapo.pt > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >-------------------------------------------------- DISCLAIMER\ This e-mail, and any attachments thereto, is intende... {{dropped}}
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...
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...Since your matrix elements must be all the same shape (rows x columns) for you to sum them, you could store them in an array (see ?array) rather than a list. Then all you need do is apply 'sum' to two dimensions of the array. Here's a one-liner that converts your list into an array (by unlisting it and then packing into an array with the right three dimensions) and then runs apply(...,c(1,2),sum) to get the answer you want: First some sample data: > foo [[1]] [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 [[2]] [,1] [,2] [,3] [1,] 0.1666667 0.5000000 0.8333333 [2,] 0.3333333 0.6666667 1.0000000 [[3]] [,1] [,2] [,3] [1,] 0.5 0.5 0.5 [2,] 0.5 0.5 0.5 [[4]] [,1] [,2] [,3] [1,] 0.01 0.01 0.01 [2,] 0.01 0.01 0.01 > apply(array(unlist(foo),c(dim(foo[[1]])[1],dim(foo[[1]])[2],length(foo))),c(1,2),sum) [,1] [,2] [,3] [1,] 1.676667 4.010000 6.343333 [2,] 2.843333 5.176667 7.510000 I'm sure there's a better way. Baz
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
How about something like:> matlist <- list(matrix(1:16, 4, 4), matrix(2, 4, 4)) > do.call("+", matlist)[,1] [,2] [,3] [,4] [1,] 3 7 11 15 [2,] 4 8 12 16 [3,] 5 9 13 17 [4,] 6 10 14 18>-roger _______________________________ UCLA Department of Statistics http://www.stat.ucla.edu/~rpeng On Wed, 23 Apr 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... > -- > > > http://adsl.sapo.pt > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >
Won't work if the list has more than 2 matrices. Andy> From: Roger Peng [mailto:rpeng at stat.ucla.edu] > > How about something like: > > > matlist <- list(matrix(1:16, 4, 4), matrix(2, 4, 4)) > > do.call("+", matlist) > [,1] [,2] [,3] [,4] > [1,] 3 7 11 15 > [2,] 4 8 12 16 > [3,] 5 9 13 17 > [4,] 6 10 14 18 > > > > -roger > _______________________________ > UCLA Department of Statistics > http://www.stat.ucla.edu/~rpeng > > On Wed, 23 Apr 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... > > -- > > > > > > http://adsl.sapo.pt > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments, cont... {{dropped}}
On Wednesday 23 April 2003 12:57 pm, 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...If the list is not too long, perhaps sumlist <- function(x) if (length(x) > 2) sumlist(x[1:2]) + sumlist(x[-(1:2)]) else do.call("+", x) sumlist(<whatever your list is>) -Deepayan
Dear Luis, On Wednesday 23 April 2003 12:57 pm, Luis Silva wrote: > 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... Here's a recursive function that works with a list of two or more matrices: sumMatrices <- function(matrices){ if (length(matrices) > 2) matrices[[1]] + Recall(matrices[-1]) else matrices[[1]] + matrices[[2]] } Regards, John ----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox
> From: Peter Dalgaard BSA [mailto:p.dalgaard at biostat.ku.dk] > > Barry Rowlingson <B.Rowlingson at lancaster.ac.uk> writes: > > > 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... > > > Here's a one-liner that converts your list into an array (by > > unlisting it and then packing into an array with the right three > > dimensions) and then runs apply(...,c(1,2),sum) to get the > answer you > > want: > > Didn't someone do an abind() function at some point? (Generalizing > cbind/rbind)I believe there's an abind for Splus on StatLib, if I remember correctly by Tony Plate & Rich Heiberger. Do not believe it was made available for R, though I believe it'd be very useful. [snipped]> However, aren't we ignoring the obvious?: > > s<-0;(for(a in l)s<-s+a)Indeed! (I guess somehow the evil of for loops in the old S in deeply engrained in some of us.) Altough I like Sundar's version, too. Cheers, Andy ------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments, cont... {{dropped}}