[R] sum of unknown number of matrices
Barry Rowlingson
b.rowlingson at lancaster.ac.uk
Wed Jun 4 17:18:30 CEST 2008
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
More information about the R-help
mailing list