Re: [R] sweep() and recycling

From: Robin Hankin <>
Date: Mon 20 Jun 2005 - 23:38:13 EST

Hi Heather

thanks for this. (I have messed up quote level somewhere. Sorry for that).

On Jun 20, 2005, at 02:01 pm, Heather Turner wrote:
>>>> Robin Hankin <> 06/20/05 10:32am >>>
>> Hi
>> [snip]
>> I would expect sweep() to give
>> me a warning if the elements don't recycle nicely
>> behave as expected, But
>> sweep(X,1,1:5,"+")
>> and
>> sweep(X,2,1:5,"+")
>> are identical!
> ...which is also expected since the matrix is square. Perhaps the
>> following will help you see why:

> matrix(1:5,6,6)
> matrix(1:5,6,6, byrow = TRUE)

Yes, after thinking about your example for a few minutes, it was a bit foolish of me
to use a square matrix to illustrate this problem. Thinking about it, it is the coincidence that ncol(X) %% n == nrow(X)%%n == 1 that
caused the phenomenon I remarked on [here n=length(x)].

I should have known better, as I use this phenomenon deliberately in magic square
construction! (although not by using sweep()).

> I agree with your main point however, it would be useful if 'sweep'
> would give a warning when the length of the vector of statistics to be
> swept out was not a sub-multiple or multiple of the corresponding array
> dimension.

yes, that would be good. Can anyone think of a situation in which one would want to ignore
such a warning? Conversely, does anyone deliberately use sweep() with

   non-recycling vector?


Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743

______________________________________________ mailing list
PLEASE do read the posting guide!
Received on Tue Jun 21 00:00:50 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:54 EST