Re: [R] sum of unknown number of matrices

From: Berwin A Turlach <berwin_at_maths.uwa.edu.au>
Date: Wed, 04 Jun 2008 23:33:10 +0800

G'day Shubha,

On Wed, 4 Jun 2008 20:23:35 +0530
"Shubha Vishwanath Karanth" <shubhak_at_ambaresearch.com> wrote:

> Something like do.call("+",l) is not working...why is this?

Well, as the error message says, "+" is either a unary or a binary operator, i.e. it takes either one or two arguments, but not more.

> I may not be knowing the number of matrices in the list...

This is perhaps a bit complicated but it works:

R> a=b=c=d=matrix(1:4,2,2)
R> l=list(a,b,c,d)
R> library(abind)  ## may have to install this package first
R> apply(do.call(abind, list(l, along=3)), 1:2, sum)
     [,1] [,2]

[1,] 4 12
[2,] 8 16

HTH. Cheers,

        Berwin


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. Received on Wed 04 Jun 2008 - 16:52:48 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 04 Jun 2008 - 17:30:37 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive