No doubt an easy answer to this exists. I have a data frame which comprises 4 matrices and 2 vectors: year(1024,6),growth(1024,6),ycens(1024,6),gcens(1024,6),yinit(1024),ginit(1024) The names of the 26 columns get denoted as y.1 to y.6, g.1 to g.6, yc.1 to yc.6, gc.1 to gc.6, yi and gi as these are the internal names. I wish to make a subset of the data using the logical vector R1(length 1024) so that only the cases where R1 is TRUE are included. I can do this by: R1data<-alldata[R1] that gives a vector 7800 long (there are 300 TRUE's in R1) so I do R1data<-as.data.frame(alldata[R1]) which gives a matrix 7800x1 so I write: R1data<-alldata[R1];dim(R1data)<-c(300,26) which gives the data in the right form except I haven't inherited the names at all. (a) How to do that? (b) Is there a convenient one-liner (or more) where I can just write something like the first expression (which is intuitive) but where it can inherit the column names (and possibly further attributes) (c) Is there a way within a data frame to refer conveniently to a group of columns (ie one of the constituent matrices in the case above)? TIA \John -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Wed, 20 Jan 1999, John Logsdon wrote:> No doubt an easy answer to this exists. > > I have a data frame which comprises 4 matrices and 2 vectors: > > year(1024,6),growth(1024,6),ycens(1024,6),gcens(1024,6),yinit(1024),ginit(1024) > > The names of the 26 columns get denoted as y.1 to y.6, g.1 to g.6, yc.1 to > yc.6, gc.1 to gc.6, yi and gi as these are the internal names. > > I wish to make a subset of the data using the logical vector R1(length > 1024) so that only the cases where R1 is TRUE are included. I can do this > by: > > R1data<-alldata[R1]R1data<-alldata[R1,] is what you want (note the comma)> (c) Is there a way within a data frame to refer conveniently to a group of > columns (ie one of the constituent matrices in the case above)? >alldata[,c(1,3,5,11:20)] would give the first, third, fifth, and eleventh through twentieth columns, or you can refer to them by name: eg alldata[,c("y.2","gc.4","gi")] Thomas Lumley Assistant Professor, Biostatistics University of Washington, Seattle -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._