[R] array addition doesn't recycle!

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Mar 31 16:59:36 CEST 2004


The recycling rules are documented and this is not amongst them.
Computer packages do have a tendency to follow their rules rather than 
read your mind.

I suspect A + as.vector(a) is what you intended.

On Wed, 31 Mar 2004, Tamas Papp wrote:

> Hi,
> 
> I have noticed the following:
> 
> > a <- array(1:4, c(2, 2))
> > A <- array(1:4, c(2,2,2))
> > A + a
> Error in A + a : non-conformable arrays
> 
> It works with a matrix + a vector, why doesn't it work with arrays?
> Am I missing something?
> 
> How would you do the above operation efficiently (ie I need to add a
> matrix to each "plane" of 3-dim array)?  At the moment I am using
> something like
> 
> A + array(a, c(2,2,2))
> 
> but it doesn't seem that efficient.

Why do you think is `not that efficient'?  Does you have a need to save 
microseconds?

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list