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.
resizing data
8 messages · Morway, Eric, arun, Brian Diggs +1 more
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.
I played around with your example on the smaller dataset, and it seemed like it was doing what I wanted. However, applying it to the larger problem, I didn't get a resized 2D dataset that preserved the order I was hoping for. Hopefully the following illustrates the larger problem: x<-matrix(0,nrow=29328,ncol=7) # Now set the 70,000th element to 1 to find out where it ends up? # Iterating on columns first, then rows, the 70,000th element is # at index [10000,7] x[10000,7]<-1 y<-t(matrix(x,nrow=546)) dim(y) # [1] 376 546 # Does 1 appear at index [129,112] as I expect # (128 complete rows x 546 cols = 69888 + 112 = 70,000) thus, row 129, col 112 y[129,112] # [1] 0 # No, so where is it? grep(1,y) # [1] 123293 Where is that? -- View this message in context: http://r.789695.n4.nabble.com/resizing-data-tp4656653p4656662.html Sent from the R help mailing list archive at Nabble.com.
On 1/25/2013 2:29 PM, emorway wrote:
I played around with your example on the smaller dataset, and it seemed like it was doing what I wanted. However, applying it to the larger problem, I didn't get a resized 2D dataset that preserved the order I was hoping for. Hopefully the following illustrates the larger problem: x<-matrix(0,nrow=29328,ncol=7) # Now set the 70,000th element to 1 to find out where it ends up? # Iterating on columns first, then rows, the 70,000th element is # at index [10000,7] x[10000,7]<-1 y<-t(matrix(x,nrow=546)) dim(y) # [1] 376 546 # Does 1 appear at index [129,112] as I expect # (128 complete rows x 546 cols = 69888 + 112 = 70,000) thus, row 129, col 112 y[129,112] # [1] 0 # No, so where is it? grep(1,y) # [1] 123293 Where is that?
which(y==1, arr.ind=TRUE) # row col # [1,] 341 328 You need an extra transposition y <- t(matrix(t(x), nrow=546)) y[129,112] # [1] 1 which(y==1, arr.ind=TRUE) # row col # [1,] 129 112
Brian S. Diggs, PhD Senior Research Associate, Department of Surgery Oregon Health & Science University
On Jan 25, 2013, at 2:29 PM, emorway wrote:
I played around with your example on the smaller dataset, and it seemed like it was doing what I wanted. However, applying it to the larger problem, I didn't get a resized 2D dataset that preserved the order I was hoping for. Hopefully the following illustrates the larger problem: x<-matrix(0,nrow=29328,ncol=7) # Now set the 70,000th element to 1 to find out where it ends up? # Iterating on columns first, then rows, the 70,000th element is # at index [10000,7]
In an R matrix the "70,000"th element of a matrix with those dimensions would be in the 3rd column.
x[10000,7]<-1
That's not the 70000th element. its the (29328*6+1)th element. Given your demonstrated misconceptions about R matrices and their column-major ordering I do not think I need to proceed further. Do some more self-study on R matrices.
y<-t(matrix(x,nrow=546)) dim(y) # [1] 376 546 # Does 1 appear at index [129,112] as I expect # (128 complete rows x 546 cols = 69888 + 112 = 70,000) thus, row 129, col 112 y[129,112] # [1] 0 # No, so where is it? grep(1,y) # [1] 123293 Where is that? -- View this message in context: http://r.789695.n4.nabble.com/resizing-data-tp4656653p4656662.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.
David Winsemius Alameda, CA, USA
which(x==1,arr.ind=TRUE) ?????? row col #[1,] 10000?? 7 ?y<-t(matrix(x,nrow=546)) ?which(y==1,arr.ind=TRUE) #???? row col #[1,] 341 328 A.K. ----- Original Message ----- From: emorway <emorway at usgs.gov> To: r-help at r-project.org Cc: Sent: Friday, January 25, 2013 5:29 PM Subject: Re: [R] resizing data I played around with your example on the smaller dataset, and it seemed like it was doing what I wanted.? However, applying it to the larger problem, I didn't? get a resized 2D dataset that preserved the order I was hoping for. Hopefully the following illustrates the larger problem: x<-matrix(0,nrow=29328,ncol=7) # Now set the 70,000th element to 1 to find out where it ends up? # Iterating on columns first, then rows, the 70,000th element is # at index [10000,7] x[10000,7]<-1 y<-t(matrix(x,nrow=546)) dim(y) # [1] 376 546 # Does 1 appear at index [129,112] as I expect # (128 complete rows x 546 cols = 69888 + 112 = 70,000) thus, row 129, col 112 y[129,112] # [1] 0 # No, so where is it? grep(1,y) # [1] 123293 Where is that? -- View this message in context: http://r.789695.n4.nabble.com/resizing-data-tp4656653p4656662.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.
Hi, Inline: ----- Original Message ----- From: emorway <emorway at usgs.gov> To: r-help at r-project.org Cc: Sent: Friday, January 25, 2013 5:29 PM Subject: Re: [R] resizing data I played around with your example on the smaller dataset, and it seemed like it was doing what I wanted.? However, applying it to the larger problem, I didn't? get a resized 2D dataset that preserved the order I was hoping for. Hopefully the following illustrates the larger problem: x<-matrix(0,nrow=29328,ncol=7) # Now set the 70,000th element to 1 to find out where it ends up? # Iterating on columns first, then rows, the 70,000th element is # at index [10000,7] x[10000,7]<-1 y<-t(matrix(x,nrow=546)) It should be: x2<-unlist(x) which(x2==1) #[1] 185968 ?29328*6+10000 #[1] 185968 If it was transposed: x3<-unlist(t(x)) ?which(x3==1) #[1] 70000 A.K. dim(y) # [1] 376 546 # Does 1 appear at index [129,112] as I expect # (128 complete rows x 546 cols = 69888 + 112 = 70,000) thus, row 129, col 112 y[129,112] # [1] 0 # No, so where is it? grep(1,y) # [1] 123293 Where is that? -- View this message in context: http://r.789695.n4.nabble.com/resizing-data-tp4656653p4656662.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.