Hans Thompson
2012-Oct-15 23:58 UTC
[R] Chopping a two column data frame by rows into a three dimensional array.
If I have a two column data frame like:> dat <- cbind("x"=c(1:100),"y"=c(100:1))How can I create an array that splits every ten rows of that data frame into a third dimension of an array so that:> newarray[,,1],,1 x y 1 100 2 99 3 98 ... ... 10 91 ,,2 x y 11 90 12 89 ... ... ... Thanks. [[alternative HTML version deleted]]
arun
2012-Oct-16 00:34 UTC
[R] Chopping a two column data frame by rows into a three dimensional array.
HI, May be this: dat <- cbind(x=c(1:100),y=c(100:1)) newarray<-array(do.call(cbind,lapply(split(dat,rep(1:10,each=10)),function(x) cbind(x=x[1:10],y=x[11:20]))),dim=c(10,2,10)) colnames(newarray)<-c("x","y") ?newarray[,,1] ? # ??? x?? y ?#[1,]? 1 100 ?#[2,]? 2? 99 ?#[3,]? 3? 98 ?#[4,]? 4? 97 ?#[5,]? 5? 96 ?#[6,]? 6? 95 ?#[7,]? 7? 94 ?#[8,]? 8? 93 ?#[9,]? 9? 92 #[10,] 10? 91 A.K. ----- Original Message ----- From: Hans Thompson <hans.thompson1 at gmail.com> To: r-help at r-project.org Cc: Sent: Monday, October 15, 2012 7:58 PM Subject: [R] Chopping a two column data frame by rows into a three dimensional array. If I have a two column data frame like:> dat <- cbind("x"=c(1:100),"y"=c(100:1))How can I create an array that splits every ten rows of that data frame into a third dimension of an array so that:> newarray[,,1],,1 x? y 1? 100 2? 99 3? 98 ...? ... 10? 91 ,,2 x? ? y 11? 90 12? 89 ...? ... ... Thanks. ??? [[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.
Jeff Newmiller
2012-Oct-16 01:33 UTC
[R] Chopping a two column data frame by rows into a three dimensional array.
aperm(array(dat,c(10,10,2)),c(1,3,2)) --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Hans Thompson <hans.thompson1 at gmail.com> wrote:>If I have a two column data frame like: > >> dat <- cbind("x"=c(1:100),"y"=c(100:1)) > >How can I create an array that splits every ten rows of that data frame >into a third dimension of an array so that: > >> newarray[,,1] > >,,1 > >x y >1 100 >2 99 >3 98 >... ... >10 91 > > >,,2 > >x y >11 90 >12 89 >... ... > > >... > >Thanks. > > [[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.