Hello everyone, I need reshape an array. For example, if we have next array:> a <- c(1,2,3,4,5,6,7,8,9,10,11,12) > dim(a) <- c(2,2,3) > a, , 1 [,1] [,2] [1,] 1 3 [2,] 2 4 , , 2 [,1] [,2] [1,] 5 7 [2,] 6 8 , , 3 [,1] [,2] [1,] 9 11 [2,] 10 12 I need to get next matrices: 1 2 3 4 5 6 7 8 9 10 11 12 1 3 2 4 5 7 6 8 9 11 10 12 It exist any function that can be able to do it? Thanks and sorry for my english. [[alternative HTML version deleted]]
Hello everyone, I need reshape an array. For example, if we have next array:> a <- c(1,2,3,4,5,6,7,8,9,10,11,12) > dim(a) <- c(2,2,3) > a, , 1 [,1] [,2] [1,] 1 3 [2,] 2 4 , , 2 [,1] [,2] [1,] 5 7 [2,] 6 8 , , 3 [,1] [,2] [1,] 9 11 [2,] 10 12 I need to get next matrices: 1 2 3 4 5 6 7 8 9 10 11 12 1 3 2 4 5 7 6 8 9 11 10 12 Does any function exist that can be able to do it ? Thanks in advance and sorry for my english. [[alternative HTML version deleted]]
Does this do what you want:> a <- 1:12 > dim(a) <- c(2,2,3) > a, , 1 [,1] [,2] [1,] 1 3 [2,] 2 4 , , 2 [,1] [,2] [1,] 5 7 [2,] 6 8 , , 3 [,1] [,2] [1,] 9 11 [2,] 10 12> dim(a) <- c(4,3) > (b <- t(a))[,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 5 6 7 8 [3,] 9 10 11 12> b[,c(1,3,2,4)][,1] [,2] [,3] [,4] [1,] 1 3 2 4 [2,] 5 7 6 8 [3,] 9 11 10 12> > >On Tue, Jul 1, 2008 at 6:05 AM, Francisco Javier Santos Alamillos <fsantos at ujaen.es> wrote:> Hello everyone, > > I need reshape an array. For example, if we have next array: > >> a <- c(1,2,3,4,5,6,7,8,9,10,11,12) >> dim(a) <- c(2,2,3) >> a > , , 1 > > [,1] [,2] > [1,] 1 3 > [2,] 2 4 > > , , 2 > > [,1] [,2] > [1,] 5 7 > [2,] 6 8 > > , , 3 > > [,1] [,2] > [1,] 9 11 > [2,] 10 12 > > I need to get next matrices: > > 1 2 3 4 > 5 6 7 8 > 9 10 11 12 > > 1 3 2 4 > 5 7 6 8 > 9 11 10 12 > > > Does any function exist that can be able to do it ? > > Thanks in advance and sorry for my english. > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
on 07/01/2008 04:58 AM Francisco Javier Santos Alamillos wrote:> Hello everyone, > > I need reshape an array. For example, if we have next array: > >> a <- c(1,2,3,4,5,6,7,8,9,10,11,12) >> dim(a) <- c(2,2,3) >> a > , , 1 > > [,1] [,2] > [1,] 1 3 > [2,] 2 4 > > , , 2 > > [,1] [,2] > [1,] 5 7 > [2,] 6 8 > > , , 3 > > [,1] [,2] > [1,] 9 11 > [2,] 10 12 > > I need to get next matrices: > > 1 2 3 4 > 5 6 7 8 > 9 10 11 12 > > 1 3 2 4 > 5 7 6 8 > 9 11 10 12 > > > It exist any function that can be able to do it? > > Thanks and sorry for my english.Keep in mind, as you seem to recognize, that a matrix and an array, is a vector with a dim attribute. Thus, to reshape an array or matrix, you need to alter the dim attribute, perhaps with a transpose for ordering purposes. There is more than one way to do this, but two would be: > t(matrix(a, 4, 3)) [,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 5 6 7 8 [3,] 9 10 11 12 > matrix(a, 3, 4, byrow = TRUE) [,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 5 6 7 8 [3,] 9 10 11 12 HTH, Marc Schwartz