Re: [R] sweep() and recycling

From: Heather Turner <Heather.Turner_at_warwick.ac.uk>
Date: Mon 20 Jun 2005 - 23:01:07 EST

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 Received on Mon Jun 20 23:49:38 2005

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