[R] sweep() and recycling
Robin Hankin
r.hankin at noc.soton.ac.uk
Mon Jun 20 15:38:13 CEST 2005
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 <r.hankin at noc.soton.ac.uk> 06/20/05 10:32am >>>
>> Hi
>>
>> [snip]
>> I would expect sweep() to give
>> me a warning if the elements don't recycle nicely
>
[snip]
>
>> 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
More information about the R-help
mailing list