Re: R-alpha: Recycling

Luke Tierney (
Mon, 24 Feb 1997 10:33:07 -0600 (CST)

From: Luke Tierney <>
Message-Id: <>
Subject: Re: R-alpha: Recycling
To: (Bill Venables)
Date: Mon, 24 Feb 1997 10:33:07 -0600 (CST)
In-Reply-To: <> from "Bill Venables" at Feb 24, 97 07:40:51 pm

> Jim Lindsey writes:
>  > When I condemned recycling vectors, I did not mean to include
>  > scalars as well.
> S (and R) does not have a separate scalar type.
>  > y <- x+1
>  > should not create problems and should be allowed, as in GLIM.
>  > y <- x+z
>  > where x and z have different lengths, should be an error
>  > instead of recycling the shorter one.  Jim
> But this rule is exceptionally useful.  For example consider a
> matrix product involving a diagonal matrix, say diag(d) %*% X.
> You can either do it that way or simply by (d * X) using the
> recycling rule.  If X is 10000 x 7, say, so d is a vector of
> length 10000, which is quicker and uses less memory?

You are relying on TWO things here: recycling and column-major storage
of arrays. Arguably, both are integral parts of S and so any S
programmer worth their salt should always be aware of them. But f I
encountered a expression (d * X) in code I was debugging I would have
to think pretty hard to become fully aware of the subtleties of its
implication. It would be a good idea at least to add a clear comment
reminding the reader that this depends on recycling and column-major
storage. An alternative with equal space complexity is


I never remember how sweep works and always have to look at the help
page (and I hope I read it right in wipping this out :-)), but the
expression tells me that right up front without requiring a
comment. Also, this version works if d is on the right by changing the
margin index. A clear drawback of using sweep is that it is likely to
be less time efficient. But in theory at least, an intelligent
preprocessing step (a.k.a. compilation) could turn a sweep with a
known function argument into something efficient.


r-testers mailing list -- For info or help, send "info" or "help",
To [un]subscribe, send "[un]subscribe"
(in the "body", not the subject !)  To: