Undoubtedly this question has been asked before, I just can't seem to find the combination of search terms to produce it. I'm trying to resize a dataset that is pulled into R using read.table. However, I think the same problem can be produced using matrix: x<-matrix(1:64,8) x # [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] #[1,] 1 9 17 25 33 41 49 57 #[2,] 2 10 18 26 34 42 50 58 #[3,] 3 11 19 27 35 43 51 59 #[4,] 4 12 20 28 36 44 52 60 #[5,] 5 13 21 29 37 45 53 61 #[6,] 6 14 22 30 38 46 54 62 #[7,] 7 15 23 31 39 47 55 63 #[8,] 8 16 24 32 40 48 56 64 The true order of data in the larger problem I'm working with is actually transposed, like so: x<-t(x) x # [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] #[1,] 1 2 3 4 5 6 7 8 #[2,] 9 10 11 12 13 14 15 16 #[3,] 17 18 19 20 21 22 23 24 #[4,] 25 26 27 28 29 30 31 32 #[5,] 33 34 35 36 37 38 39 40 #[6,] 41 42 43 44 45 46 47 48 #[7,] 49 50 51 52 53 54 55 56 #[8,] 57 58 59 60 61 62 63 64 I'm trying to resize the data (in this example, a matrix) to say a 16 x 4 matrix while preserving the consecutive order of the individual elements in a left-to-right top-to-bottom fashion. The example below is wrong because the first row should be "1 2 3 4", how can I make this happen? It would also be nice to make a 4 x 16 matrix where the first row contains the values of x[1,1:8] followed by x[2,1:8]. I'm guessing there is a 1 liner of R code for this type of thing so I don't have to resort to nested for loops? y<-matrix(x,nrow=16,ncol=4) y # [,1] [,2] [,3] [,4] # [1,] 1 3 5 7 # [2,] 9 11 13 15 # [3,] 17 19 21 23 # [4,] 25 27 29 31 # [5,] 33 35 37 39 # [6,] 41 43 45 47 # [7,] 49 51 53 55 # [8,] 57 59 61 63 # [9,] 2 4 6 8 #[10,] 10 12 14 16 #[11,] 18 20 22 24 #[12,] 26 28 30 32 #[13,] 34 36 38 40 #[14,] 42 44 46 48 #[15,] 50 52 54 56 #[16,] 58 60 62 64 -- View this message in context: http://r.789695.n4.nabble.com/resizing-data-tp4656653.html Sent from the R help mailing list archive at Nabble.com.
On Jan 25, 2013, at 1:12 PM, emorway wrote:> Undoubtedly this question has been asked before, I just can't seem to find > the combination of search terms to produce it. I'm trying to resize a > dataset that is pulled into R using read.table. However, I think the same > problem can be produced using matrix: > > x<-matrix(1:64,8) > x > # [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] > #[1,] 1 9 17 25 33 41 49 57 > #[2,] 2 10 18 26 34 42 50 58 > #[3,] 3 11 19 27 35 43 51 59 >snipped> > The true order of data in the larger problem I'm working with is actually > transposed, like so: > x<-t(x) > x > # [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] > #[1,] 1 2 3 4 5 6 7 8 > #[2,] 9 10 11 12 13 14 15 16 > #[3,] 17 18 19 20 21 22 23 24 >... snipped> I'm trying to resize the data (in this example, a matrix) to say a 16 x 4 > matrix while preserving the consecutive order of the individual elements in > a left-to-right top-to-bottom fashion. The example below is wrong because > the first row should be "1 2 3 4", how can I make this happen? It would > also be nice to make a 4 x 16 matrix where the first row contains the values > of x[1,1:8] followed by x[2,1:8]. I'm guessing there is a 1 liner of R code > for this type of thing so I don't have to resort to nested for loops? > > y<-matrix(x,nrow=16,ncol=4) > y > # [,1] [,2] [,3] [,4] > # [1,] 1 3 5 7 > # [2,] 9 11 13 15 > # [3,] 17 19 21 23 > ...snipped> > What's wrong with:X1.4 <- t( matrix(x, nrow=4) ) -- David Winsemius Alameda, CA, USA
HI, It's not clear why you wanted to take the transpose and resize it afterwards. It could be done in one step as David suggested. Suppose, you wanted to get the result after you transposed the matrix: x<-matrix(1:64,8) x1<-t(x) matrix(unlist(split(x1,row(x1))),ncol=4,byrow=T) #????? [,1] [,2] [,3] [,4] ?#[1,]??? 1??? 2??? 3??? 4 ?#[2,]??? 5??? 6??? 7??? 8 ?#[3,]??? 9?? 10?? 11?? 12 ?#[4,]?? 13?? 14?? 15?? 16 ----------------------------- #or ?do.call(rbind,lapply(split(x1,row(x1)),function(x) matrix(x,nrow=2,ncol=4,byrow=T))) ??? #? [,1] [,2] [,3] [,4] ?#[1,]??? 1??? 2??? 3??? 4 ?#[2,]??? 5??? 6??? 7??? 8 ?#[3,]??? 9?? 10?? 11?? 12 ?#[4,]?? 13?? 14?? 15?? 16 -------------------------- A.K. ----- Original Message ----- From: emorway <emorway at usgs.gov> To: r-help at r-project.org Cc: Sent: Friday, January 25, 2013 4:12 PM Subject: [R] resizing data Undoubtedly this question has been asked before, I just can't seem to find the combination of search terms to produce it.? I'm trying to resize a dataset that is pulled into R using read.table.? However, I think the same problem can be produced using matrix: x<-matrix(1:64,8) x #? ? [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] #[1,]? ? 1? ? 9? 17? 25? 33? 41? 49? 57 #[2,]? ? 2? 10? 18? 26? 34? 42? 50? 58 #[3,]? ? 3? 11? 19? 27? 35? 43? 51? 59 #[4,]? ? 4? 12? 20? 28? 36? 44? 52? 60 #[5,]? ? 5? 13? 21? 29? 37? 45? 53? 61 #[6,]? ? 6? 14? 22? 30? 38? 46? 54? 62 #[7,]? ? 7? 15? 23? 31? 39? 47? 55? 63 #[8,]? ? 8? 16? 24? 32? 40? 48? 56? 64 The true order of data in the larger problem I'm working with is actually transposed, like so: x<-t(x) x #? ? [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] #[1,]? ? 1? ? 2? ? 3? ? 4? ? 5? ? 6? ? 7? ? 8 #[2,]? ? 9? 10? 11? 12? 13? 14? 15? 16 #[3,]? 17? 18? 19? 20? 21? 22? 23? 24 #[4,]? 25? 26? 27? 28? 29? 30? 31? 32 #[5,]? 33? 34? 35? 36? 37? 38? 39? 40 #[6,]? 41? 42? 43? 44? 45? 46? 47? 48 #[7,]? 49? 50? 51? 52? 53? 54? 55? 56 #[8,]? 57? 58? 59? 60? 61? 62? 63? 64 I'm trying to resize the data (in this example, a matrix) to say a 16 x 4 matrix while preserving the consecutive order of the individual elements in a left-to-right top-to-bottom fashion.? The example below is wrong because the first row should be "1 2 3 4", how can I make this happen?? It would also be nice to make a 4 x 16 matrix where the first row contains the values of x[1,1:8] followed by x[2,1:8].? I'm guessing there is a 1 liner of R code for this type of thing so I don't have to resort to nested for loops? y<-matrix(x,nrow=16,ncol=4) y #? ? ? [,1] [,2] [,3] [,4] # [1,]? ? 1? ? 3? ? 5? ? 7 # [2,]? ? 9? 11? 13? 15 # [3,]? 17? 19? 21? 23 # [4,]? 25? 27? 29? 31 # [5,]? 33? 35? 37? 39 # [6,]? 41? 43? 45? 47 # [7,]? 49? 51? 53? 55 # [8,]? 57? 59? 61? 63 # [9,]? ? 2? ? 4? ? 6? ? 8 #[10,]? 10? 12? 14? 16 #[11,]? 18? 20? 22? 24 #[12,]? 26? 28? 30? 32 #[13,]? 34? 36? 38? 40 #[14,]? 42? 44? 46? 48 #[15,]? 50? 52? 54? 56 #[16,]? 58? 60? 62? 64 -- View this message in context: http://r.789695.n4.nabble.com/resizing-data-tp4656653.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.