[R] Summing up matrices in a list

Adaikalavan Ramasamy ramasamy at cancer.org.uk
Thu Mar 17 14:18:49 CET 2005


Yes, I was thinking of the trivial problem of 2 matrices. Nice to know I
am not the only one who made the same error.

Thanks to Thomas Lumley, John Fox, Dimitris Rizopoulos for pointing this
out and many others for providing the correct solution.

Regards, Adai


On Wed, 2005-03-16 at 11:07 -0600, Marc Schwartz wrote:
> John,
> 
> That is correct. I took the example perhaps too literally, depending
> upon what Vicky requires. If indeed the data structure is comprised of
> >2 matrices, the approach using do.call() will not work.
> 
> Thanks for pointing that out.  I see that Adai had a similar idea.
> 
> Best regards,
> 
> Marc
> 
> 
> On Wed, 2005-03-16 at 12:00 -0500, John Fox wrote:
> > Dear Mark,
> > 
> > I believe that your solution won't work if there are more than two matrices
> > to sum.
> > 
> > Regards,
> >  John
> > 
> > --------------------------------
> > John Fox
> > Department of Sociology
> > McMaster University
> > Hamilton, Ontario
> > Canada L8S 4M4
> > 905-525-9140x23604
> > http://socserv.mcmaster.ca/jfox 
> > -------------------------------- 
> > 
> > > -----Original Message-----
> > > From: r-help-bounces at stat.math.ethz.ch 
> > > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Marc Schwartz
> > > Sent: Wednesday, March 16, 2005 11:30 AM
> > > To: Vicky Landsman
> > > Cc: R-Help
> > > Subject: Re: [R] Summing up matrices in a list
> > > 
> > > On Wed, 2005-03-16 at 18:21 +0200, Vicky Landsman wrote:
> > > > Dear all,
> > > > I think that my question is very simple but I failed to solve it. 
> > > > I have a list which elements are matrices like this:
> > > >  
> > > > >mylist
> > > > [[1]]
> > > >      [,1] [,2] [,3]
> > > > [1,]    1    3    5
> > > > [2,]    2    4    6
> > > > 
> > > > [[2]]
> > > >      [,1] [,2] [,3]
> > > > [1,]    7    9   11
> > > > [2,]    8   10   12
> > > > 
> > > > I'd like to create a matrix M<-mylist[[1]]+mylist[[2]]
> > > >      [,1] [,2] [,3]
> > > > [1,]    8   12   16
> > > > [2,]   10   14   18
> > > > 
> > > > Is there a way to create M without looping? 
> > > > Thanks a lot,
> > > 
> > > 
> > > > do.call("+", mylist)
> > >      [,1] [,2] [,3]
> > > [1,]    8   12   16
> > > [2,]   10   14   18
> > > 
> > > See ?do.call for more information.
> > > 
> > > HTH,
> > > 
> > > Marc Schwartz
> > > 
> > > ______________________________________________
> > > R-help at stat.math.ethz.ch mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide! 
> > > http://www.R-project.org/posting-guide.html
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list