Hello all, I have data from a csv that i need to coerce into a multi dimensional array. As you can see from my sample code i have yet to master building efficient code. I can sort of do what i want but its very cumbersome code and i know there is a better way to do it, i am just not expereinced enought yet. So for the following: #Create Data h1i1a1<-sample(1:20, 5, replace=TRUE) h1i1a2<-sample(1:20, 5, replace=TRUE) h1i1a3<-sample(1:20, 5, replace=TRUE) h1i2a1<-sample(1:20, 5, replace=TRUE) h1i2a2<-sample(1:20, 5, replace=TRUE) h1i2a3<-sample(1:20, 5, replace=TRUE) h1i3a1<-sample(1:20, 5, replace=TRUE) h1i3a2<-sample(1:20, 5, replace=TRUE) h1i3a3<-sample(1:20, 5, replace=TRUE) h1i4a1<-sample(1:20, 5, replace=TRUE) h1i4a2<-sample(1:20, 5, replace=TRUE) h1i4a3<-sample(1:20, 5, replace=TRUE) h2i1a1<-sample(1:20, 5, replace=TRUE) h2i1a2<-sample(1:20, 5, replace=TRUE) h2i1a3<-sample(1:20, 5, replace=TRUE) h2i2a1<-sample(1:20, 5, replace=TRUE) h2i2a2<-sample(1:20, 5, replace=TRUE) h2i2a3<-sample(1:20, 5, replace=TRUE) h2i3a1<-sample(1:20, 5, replace=TRUE) h2i3a2<-sample(1:20, 5, replace=TRUE) h2i3a3<-sample(1:20, 5, replace=TRUE) h2i4a1<-sample(1:20, 5, replace=TRUE) h2i4a2<-sample(1:20, 5, replace=TRUE) h2i4a3<-sample(1:20, 5, replace=TRUE) h3i1a1<-sample(1:20, 5, replace=TRUE) h3i1a2<-sample(1:20, 5, replace=TRUE) h3i1a3<-sample(1:20, 5, replace=TRUE) h3i2a1<-sample(1:20, 5, replace=TRUE) h3i2a2<-sample(1:20, 5, replace=TRUE) h3i2a3<-sample(1:20, 5, replace=TRUE) h3i3a1<-sample(1:20, 5, replace=TRUE) h3i3a2<-sample(1:20, 5, replace=TRUE) h3i3a3<-sample(1:20, 5, replace=TRUE) #Put into data frame hia<-data.frame(h1i1a1,h1i1a2,h1i1a3,h1i2a1,h1i2a2,h1i2a3,h1i3a1,h1i3a2,h1i3a3,h1i4a1,h1i4a2,h1i4a3,h2i1a1,h2i1a2,h2i1a3,h2i2a1,h2i2a2,h2i2a3,h2i3a1,h2i3a2,h2i3a3,h2i4a1,h2i4a2,h2i4a3,h3i1a1,h3i1a2,h3i1a3,h3i2a1,h3i2a2,h3i2a3,h3i3a1,h3i3a2,h3i3a3) i would like to produce a multidimensional array that looks something like h1 h2 h3 i1 X X X i2 X X X i3 X X X a = 1 , TAZ = 1:5 ->for all 5 TAZ values or each row h1 h2 h3 i1 X X X i2 X X X i3 X X X a = 2 , TAZ = i in 1:5 h1 h2 h3 i1 X X X i2 X X X i3 X X X a = 3 , TAZ = i in 1:5 Basically a matrix for all possible combination, so 27 matrices altogether. But i guess its more like an array of arrays? Again i am not being lazy, i have been working on this all day but my code, like my sample stuff is very inefficient and ultimately not working well so im not bothering to post it. I know this will be complicated so thank you in advance, seeing the end code will teach me a great deal. Thanks Cheers, JR -- View this message in context: http://n4.nabble.com/Create-a-multi-dimensional-array-from-a-data-frame-tp1068487p1068487.html Sent from the R help mailing list archive at Nabble.com.
Dennis Murphy
2010-Jan-21 23:24 UTC
[R] Create a multi dimensional array from a data frame
Hi: Before you do all of that, might one inquire about which goal you are trying to achieve? Perhaps a 27-dimensional array may not be the wisest choice of data structure for your goal. Dennis On Thu, Jan 21, 2010 at 3:06 PM, LCOG1 <jroll@lcog.org> wrote:> > Hello all, > I have data from a csv that i need to coerce into a multi dimensional > array. As you can see from my sample code i have yet to master building > efficient code. I can sort of do what i want but its very cumbersome code > and i know there is a better way to do it, i am just not expereinced > enought > yet. So for the following: > > #Create Data > h1i1a1<-sample(1:20, 5, replace=TRUE) > h1i1a2<-sample(1:20, 5, replace=TRUE) > h1i1a3<-sample(1:20, 5, replace=TRUE) > h1i2a1<-sample(1:20, 5, replace=TRUE) > h1i2a2<-sample(1:20, 5, replace=TRUE) > h1i2a3<-sample(1:20, 5, replace=TRUE) > h1i3a1<-sample(1:20, 5, replace=TRUE) > h1i3a2<-sample(1:20, 5, replace=TRUE) > h1i3a3<-sample(1:20, 5, replace=TRUE) > h1i4a1<-sample(1:20, 5, replace=TRUE) > h1i4a2<-sample(1:20, 5, replace=TRUE) > h1i4a3<-sample(1:20, 5, replace=TRUE) > h2i1a1<-sample(1:20, 5, replace=TRUE) > h2i1a2<-sample(1:20, 5, replace=TRUE) > h2i1a3<-sample(1:20, 5, replace=TRUE) > h2i2a1<-sample(1:20, 5, replace=TRUE) > h2i2a2<-sample(1:20, 5, replace=TRUE) > h2i2a3<-sample(1:20, 5, replace=TRUE) > h2i3a1<-sample(1:20, 5, replace=TRUE) > h2i3a2<-sample(1:20, 5, replace=TRUE) > h2i3a3<-sample(1:20, 5, replace=TRUE) > h2i4a1<-sample(1:20, 5, replace=TRUE) > h2i4a2<-sample(1:20, 5, replace=TRUE) > h2i4a3<-sample(1:20, 5, replace=TRUE) > h3i1a1<-sample(1:20, 5, replace=TRUE) > h3i1a2<-sample(1:20, 5, replace=TRUE) > h3i1a3<-sample(1:20, 5, replace=TRUE) > h3i2a1<-sample(1:20, 5, replace=TRUE) > h3i2a2<-sample(1:20, 5, replace=TRUE) > h3i2a3<-sample(1:20, 5, replace=TRUE) > h3i3a1<-sample(1:20, 5, replace=TRUE) > h3i3a2<-sample(1:20, 5, replace=TRUE) > h3i3a3<-sample(1:20, 5, replace=TRUE) > #Put into data frame > > hia<-data.frame(h1i1a1,h1i1a2,h1i1a3,h1i2a1,h1i2a2,h1i2a3,h1i3a1,h1i3a2,h1i3a3,h1i4a1,h1i4a2,h1i4a3,h2i1a1,h2i1a2,h2i1a3,h2i2a1,h2i2a2,h2i2a3,h2i3a1,h2i3a2,h2i3a3,h2i4a1,h2i4a2,h2i4a3,h3i1a1,h3i1a2,h3i1a3,h3i2a1,h3i2a2,h3i2a3,h3i3a1,h3i3a2,h3i3a3) > > i would like to produce a multidimensional array that looks something like > > h1 h2 h3 > i1 X X X > i2 X X X > i3 X X X > > a = 1 , TAZ = 1:5 ->for all 5 TAZ values or each row > > h1 h2 h3 > i1 X X X > i2 X X X > i3 X X X > > a = 2 , TAZ = i in 1:5 > > h1 h2 h3 > i1 X X X > i2 X X X > i3 X X X > > a = 3 , TAZ = i in 1:5 > > Basically a matrix for all possible combination, so 27 matrices > altogether. > But i guess its more like an array of arrays? Again i am not being lazy, i > have been working on this all day but my code, like my sample stuff is very > inefficient and ultimately not working well so im not bothering to post it. > I know this will be complicated so thank you in advance, seeing the end > code > will teach me a great deal. Thanks > > Cheers, > JR > -- > View this message in context: > http://n4.nabble.com/Create-a-multi-dimensional-array-from-a-data-frame-tp1068487p1068487.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
David Winsemius
2010-Jan-22 04:46 UTC
[R] Create a multi dimensional array from a data frame
On Jan 21, 2010, at 6:06 PM, LCOG1 wrote:> > Hello all, > I have data from a csv that i need to coerce into a multi > dimensional > array. As you can see from my sample code i have yet to master > building > efficient code. I can sort of do what i want but its very > cumbersome code > and i know there is a better way to do it, i am just not expereinced > enought > yet. So for the following: > > #Create Data > h1i1a1<-sample(1:20, 5, replace=TRUE) > h1i1a2<-sample(1:20, 5, replace=TRUE) > h1i1a3<-sample(1:20, 5, replace=TRUE) > h1i2a1<-sample(1:20, 5, replace=TRUE) > h1i2a2<-sample(1:20, 5, replace=TRUE) > h1i2a3<-sample(1:20, 5, replace=TRUE) > h1i3a1<-sample(1:20, 5, replace=TRUE) > h1i3a2<-sample(1:20, 5, replace=TRUE) > h1i3a3<-sample(1:20, 5, replace=TRUE) > h1i4a1<-sample(1:20, 5, replace=TRUE) > h1i4a2<-sample(1:20, 5, replace=TRUE) > h1i4a3<-sample(1:20, 5, replace=TRUE) > h2i1a1<-sample(1:20, 5, replace=TRUE) > h2i1a2<-sample(1:20, 5, replace=TRUE) > h2i1a3<-sample(1:20, 5, replace=TRUE) > h2i2a1<-sample(1:20, 5, replace=TRUE) > h2i2a2<-sample(1:20, 5, replace=TRUE) > h2i2a3<-sample(1:20, 5, replace=TRUE) > h2i3a1<-sample(1:20, 5, replace=TRUE) > h2i3a2<-sample(1:20, 5, replace=TRUE) > h2i3a3<-sample(1:20, 5, replace=TRUE) > h2i4a1<-sample(1:20, 5, replace=TRUE) > h2i4a2<-sample(1:20, 5, replace=TRUE) > h2i4a3<-sample(1:20, 5, replace=TRUE) > h3i1a1<-sample(1:20, 5, replace=TRUE) > h3i1a2<-sample(1:20, 5, replace=TRUE) > h3i1a3<-sample(1:20, 5, replace=TRUE) > h3i2a1<-sample(1:20, 5, replace=TRUE) > h3i2a2<-sample(1:20, 5, replace=TRUE) > h3i2a3<-sample(1:20, 5, replace=TRUE) > h3i3a1<-sample(1:20, 5, replace=TRUE) > h3i3a2<-sample(1:20, 5, replace=TRUE) > h3i3a3<-sample(1:20, 5, replace=TRUE) > #Put into data frame > hia<- > data > .frame > (h1i1a1 > ,h1i1a2 > ,h1i1a3 > ,h1i2a1 > ,h1i2a2 > ,h1i2a3 > ,h1i3a1 > ,h1i3a2 > ,h1i3a3 > ,h1i4a1 > ,h1i4a2 > ,h1i4a3 > ,h2i1a1 > ,h2i1a2 > ,h2i1a3 > ,h2i2a1 > ,h2i2a2 > ,h2i2a3 > ,h2i3a1 > ,h2i3a2 > ,h2i3a3 > ,h2i4a1 > ,h2i4a2 > ,h2i4a3 > ,h3i1a1,h3i1a2,h3i1a3,h3i2a1,h3i2a2,h3i2a3,h3i3a1,h3i3a2,h3i3a3)H <- array(c(h1i1a1, h1i1a2, h1i1a3, h1i2a1, h1i2a2, h1i2a3, h1i3a1, h1i3a2, h1i3a3, h1i4a1, h1i4a2, h1i4a3, h2i1a1, h2i1a2, h2i1a3, h2i2a1, h2i2a2, h2i2a3, h2i3a1, h2i3a2, h2i3a3, h2i4a1, h2i4a2, h2i4a3, h3i1a1, h3i1a2, h3i1a3, h3i2a1, h3i2a2, h3i2a3, h3i3a1, h3i3a2, h3i3a3), c(5,3,3,3) , dimnames=list(NULL, paste("i", 1:3, sep=""), paste("h", 1:3, sep=""),paste("a", 1:3, sep="") )) H Would have been a lot easier to do this: H < array( sample(1:20, 5*3*3*3, replace=TRUE), c(5,3,3,3) ) You can rearrange the names and dimensions to suit your tastes.> > i would like to produce a multidimensional array that looks > something like > > h1 h2 h3 > i1 X X X > i2 X X X > i3 X X X > > a = 1 , TAZ = 1:5 ->for all 5 TAZ values or each row > > h1 h2 h3 > i1 X X X > i2 X X X > i3 X X X > > a = 2 , TAZ = i in 1:5 > > h1 h2 h3 > i1 X X X > i2 X X X > i3 X X X > > a = 3 , TAZ = i in 1:5 > > Basically a matrix for all possible combination, so 27 matrices > altogether. > But i guess its more like an array of arrays? Again i am not being > lazy, i > have been working on this all day but my code, like my sample stuff > is very > inefficient and ultimately not working well so im not bothering to > post it. > I know this will be complicated so thank you in advance, seeing the > end code > will teach me a great deal. Thanks > > Cheers, > JR > -- > View this message in context: http://n4.nabble.com/Create-a-multi-dimensional-array-from-a-data-frame-tp1068487p1068487.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, MD Heritage Laboratories West Hartford, CT