Skip to content

array addition doesn't recycle!

1 message · Raubertas, Richard

#
Another alternative is to use the underappreciated function 
'sweep()':

sweep(A, 1:2, a, "+")

Internally this is about the same as your 'A + array(a, c(2,2,2))'.
But it has the advantage that it makes explicit what the 
relationship between the dimensions of 'A' and 'a' is.  I find 
that relying on implicit recycling tends to produce errors that
are hard to trace, and code that is hard to understand six months
later.

Rich Raubertas
Merck & Co.