Re: [R] sum of unknown number of matrices

From: Barry Rowlingson <b.rowlingson_at_lancaster.ac.uk>
Date: Wed, 04 Jun 2008 16:18:30 +0100

Shubha Vishwanath Karanth wrote:

> I need:
>

>> a+b+c+d

>
> [,1] [,2]
>
> [1,] 4 12
>
> [2,] 8 16
>
>
>
> Something like do.call("+",l) is not working...why is this?

Because do.call constructs a function call with the elements of l as arguments, so you end up with:

"+"(1:4, 1:4, 1:4, 1:4)

  but "+" only takes two arguments.

Use 'Reduce':

  > Reduce("+",l)

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

  Barry



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:53:38 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 - 18:30:41 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