[R] permuting dimensions (was do.call("dim<-" , ... ))

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Sep 13 10:10:16 CEST 2004


What has this to do with the original subject line?  Replacement functions
are not intended to be used directly, and certainly not in do.call.

See ?aperm, as in

xx <- x
dim(xx) <- c(2,2,3,3)
xx <- aperm(xx, c(1,3,2,4))
dim(xx) <- c(6, 6)
xx

as required.

BTW, you have a broken package `magic' on CRAN: please do us the courtesy
of fixing it so we don't continually have to look at it in tests. See

http://cran.r-project.org/src/contrib/checkSummary.html


On Mon, 13 Sep 2004, Robin Hankin wrote:

>   OK guys
> 
> another problem.  I have a 3D array "x" with dim(x)=c(a,a,b^2)
> and I want to rearrange the elements of x to make a matrix "y"
> with dimensions c(a*b,a*b).  Neither a nor b is known in advance.
> 
> I want the "n-th" a*a submatrix of y to be x[,,n] (where 1 <= n <=
> b^2).  Needless to say, this has gotta be vectorized!
> 
> Toy example with a=2, b=3 follows:
> 
> 
> Given:
> x <- array(1:36,c(2,2,9))
> 
> 
> I require:
> y <- matrix(as.integer(c(
>            1, 2, 5, 6, 9,10,
>            3, 4, 7, 8,11,12,
>           13,14,17,18,21,22,
>           15,16,19,20,23,24,
>           25,26,29,30,33,34,
>           27,28,31,32,35,36
>        )),6,6)
> 
> So
> identical(x[,,1] , y[1:2,1:2])
> identical(x[,,2] , y[3:4,1:2])
> [snip]
> identical(x[,,9] , y[5:6,5:6])
> 
> all return TRUE.
> 
> OBattempts:
> 
> (i)
> dim(x) <- c(4,9);x <- t(x) ; dim(x) <- c(6,6) ; y <- x
> 
> (ii)
> y <- aperm(x,c(2,1,3)) ; dim(y) <- c(6,6)
> 
> Any genius out there with some ideas?
> 
> 
> 

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




More information about the R-help mailing list