Dear all, When I coerce a vector into a multi dimensional array, I would like R to start filling the array along the last dimension, then the 2nd last etc. Let's jump straight into an example. x <- 1 : 24 y <- array(dim=c(2,2,6)) I would like to have: y[1,1,1] = 1 y[1,1,2] = 2 ... y[1,1,6] = 6 y[1,2,1] = 7 y[1,2,2] = 8 ... y[2,1,1] = 13 ... y[2,2,1] = 19 if I do y<- array(x, dim=c(2,2,6)), i think I will get y[1,1,1] = 1 y[2,1,1] = 2 (or something not I want) instead. Of course, I need a fast solution, as I am actually dealing with array of much larger size. Any input will be appreciated Thanks a lot
Stephan Kolassa
2010-Jan-12 12:13 UTC
[R] coerce vector into array - change filling sequence
Hi, you can permute array dimensions using aperm(): x <- 1 : 24 z <- array(x, dim=c(6,2,2)) y <- aperm(z,perm=c(3,2,1)) y[1,1,] HTH, Stephan Kohleth Chia schrieb:> Dear all, > > When I coerce a vector into a multi dimensional array, I would like R to start filling the array along the last dimension, then the 2nd last etc. > Let's jump straight into an example. > > x <- 1 : 24 > y <- array(dim=c(2,2,6)) > > I would like to have: > y[1,1,1] = 1 > y[1,1,2] = 2 > ... > y[1,1,6] = 6 > y[1,2,1] = 7 > y[1,2,2] = 8 > ... > y[2,1,1] = 13 > ... > y[2,2,1] = 19 > > if I do y<- array(x, dim=c(2,2,6)), i think I will get > y[1,1,1] = 1 > y[2,1,1] = 2 > (or something not I want) instead. > > Of course, I need a fast solution, as I am actually dealing with array of much larger size. > Any input will be appreciated > Thanks a lot > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >