Re: [R] sum of unknown number of matrices

From: Berwin A Turlach <>
Date: Wed, 04 Jun 2008 23:33:10 +0800

G'day Shubha,

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

> Something like"+",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(, list(l, along=3)), 1:2, sum)
     [,1] [,2]

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

HTH. Cheers,

        Berwin mailing list PLEASE do read the posting guide 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 Please read the posting guide before posting to the list.

list of date sections of archive