[R] sum of unknown number of matrices

Shubha Vishwanath Karanth shubhak at ambaresearch.com
Wed Jun 4 17:24:07 CEST 2008


Thanks all...Reduce() is the new function I learnt today... Thanks...

BR, Shubha
Shubha Karanth | Amba Research
Ph +91 80 3980 8031 | Mob +91 94 4886 4510 
Bangalore * Colombo * London * New York * San José * Singapore * www.ambaresearch.com
-----Original Message-----
From: Barry Rowlingson [mailto:b.rowlingson at lancaster.ac.uk] 
Sent: Wednesday, June 04, 2008 8:49 PM
To: Shubha Vishwanath Karanth
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] sum of unknown number of matrices

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


This e-mail may contain confidential and/or privileged i...{{dropped:10}}



More information about the R-help mailing list