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

Robin Hankin rksh at soc.soton.ac.uk
Mon Sep 13 08:58:51 CEST 2004


  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?


-- 
Robin Hankin
Uncertainty Analyst
Southampton Oceanography Centre
SO14 3ZH
tel +44(0)23-8059-7743
initialDOTsurname at soc.soton.ac.uk (edit in obvious way; spam precaution)




More information about the R-help mailing list