Re: [R] sweep() and recycling

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Tue 21 Jun 2005 - 01:58:15 EST

The issue here is that the equivalent command array(1:5, c(6,6)) (to matrix(1:5,6,6)) gives no warning, and sweep uses array().

I am not sure either should: fractional recycling was normally allowed in S3 (S4 tightened up a bit).

Perhaps someone who thinks sweep() should warn could contribute a tested patch?

On Mon, 20 Jun 2005, Heather Turner wrote:

>
>
> Dr H Turner
> Research Assistant
> Dept. of Statistics
> The University of Warwick
> Coventry
> CV4 7AL
>
> Tel: 024 76575870
>
>>>> Robin Hankin <r.hankin@noc.soton.ac.uk> 06/20/05 10:32am >>>
> Hi
>
>
> I had a hard-to-find bug in some of my code the other day, which I
> eventually
> traced to my misusing of sweep().
>
> I would expect sweep() to give
> me a warning if the elements don't recycle nicely, but
>
> X <- matrix(1:36,6,6)
> sweep(X,1,1:5,"+")
> [,1] [,2] [,3] [,4] [,5] [,6]
> [1,] 2 9 16 23 30 32
> [2,] 4 11 18 25 27 34
> [3,] 6 13 20 22 29 36
> [4,] 8 15 17 24 31 38
> [5,] 10 12 19 26 33 40
> [6,] 7 14 21 28 35 37
>
> gives no warning, even though (5,36)=1.
>
> Also,
>
>
> sweep(X,1,c(1,1000),"+")
>
> and
>
> sweep(X,2,c(1,1000),"+")
>
>
> 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)
> [,1] [,2] [,3] [,4] [,5] [,6]
> [1,] 1 2 3 4 5 1
> [2,] 2 3 4 5 1 2
> [3,] 3 4 5 1 2 3
> [4,] 4 5 1 2 3 4
> [5,] 5 1 2 3 4 5
> [6,] 1 2 3 4 5 1
> Warning message:
> data length [5] is not a sub-multiple or multiple of the number of rows
> [6] in matrix
>> matrix(1:5,6,6, byrow = TRUE)
> [,1] [,2] [,3] [,4] [,5] [,6]
> [1,] 1 2 3 4 5 1
> [2,] 2 3 4 5 1 2
> [3,] 3 4 5 1 2 3
> [4,] 4 5 1 2 3 4
> [5,] 5 1 2 3 4 5
> [6,] 1 2 3 4 5 1
> Warning message:
> data length [5] is not a sub-multiple or multiple of the number of rows
> [6] in matrix
> i.e. recycling a vector of length 5 down the columns gives the same
> result as recycling the same vector along the rows.
>
> 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.
>
> Heather
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

-- 
Brian D. Ripley,                  ripley@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

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Tue Jun 21 02:06:01 2005

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