Kaspar Pflugshaupt
2001-Dec-19 11:08 UTC
[R] How to create a data.frame "like" another, but longer?
Hello, does anyone know of a quick way to create a data frame "like" another, but with more rows? What I'd like to do is this: if mydata is a data.frame like a b c 1 TRUE yes 2 FALSE no 3 TRUE yes I'd like to get mydata2 with the same column names and column types, but without the values and with more rows. All I could think of was to manually do mydata2 <- maydata, then repeat mydata2 <- rbind(mydata2, mydata2) until mydata2 is long enough, then cut to desired length and overwrite with new data. Not exactly elegant... Is there a R function or some handy trick to achieve this? Cheers Kaspar -- Kaspar Pflugshaupt Geobotanisches Institut Zuerichbergstr. 38 CH-8044 Zuerich Tel. ++41 1 632 43 19 Fax ++41 1 632 12 15 mailto:pflugshaupt at geobot.umnw.ethz.ch privat:pflugshaupt at mails.ch http://www.geobot.umnw.ethz.ch -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Ott Toomet
2001-Dec-19 14:22 UTC
[R] How to create a data.frame "like" another, but longer?
Hi, How you have thought to input your data into the new dataframe? The following assumes you have new data in the form of vectors. A (slightly) more elegant way to do it is through lists. Remember that data.frame is actually a list with components with similar length. So you could do something as:> mylist <- mydata > mylist$a <- c(1,2,3,4,5) > mylist$b <- c(TRUE,TRUE,TRUE,TRUE,FALSE)...> mydata2 <- as.data.frame(mylist)Here you have to ensure that the data vectors have equal length, the new dataframe will automatically have that many rows. Basically here you are manually making the new dataframe, the only thing which is kept is the order of the variables. The new types are created automatically unless you coerce the new vectors into the old form (using something like as.factor( ..., levels=...)). Perhaps it helps. Ott Toomet On Wed, 19 Dec 2001, Kaspar Pflugshaupt wrote:> Hello, > > does anyone know of a quick way to create a data frame "like" another, but > with more rows? > > What I'd like to do is this: > > if mydata is a data.frame like > > a b c > 1 TRUE yes > 2 FALSE no > 3 TRUE yes > > I'd like to get mydata2 with the same column names and column types, but > without the values and with more rows. > > All I could think of was to manually do mydata2 <- maydata, then repeat > mydata2 <- rbind(mydata2, mydata2) until mydata2 is long enough, then cut to > desired length and overwrite with new data. Not exactly elegant... > > Is there a R function or some handy trick to achieve this? > > > Cheers > > Kaspar > >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Kaspar Pflugshaupt
2001-Dec-19 14:33 UTC
[R] How to create a data.frame "like" another, but longer?
On 19.12.2001 15:03 Uhr, Douglas Bates wrote:> Use indexing. A surprising result in the S language is that a > `subset' can be longer than the original set. > > In your case > > ind <- c(1:nrow(mydata), rep(1, n - nrow(mydata))) > mydata2 <- mydata[ind, ] > # overwrite the data here > > The reason for making the indices of the form 1, 2, 3, 1, 1, 1, ..., 1 > is to ensure that all levels of factors get represented in the > extracted data. That may be unnecessary. You could experiment with > > mydata2 <- mydata[rep(1, n), ] > > and see if that works properly.Thanks! I guessed there would be something simple like this... There seems to be no end to the surprisingly elegant indexing solutions in R! I tried it out and ended up with f.expand.df <- function(df, newrows) { oldrows <- nrow(df) new.index <- rep(1:oldrows, newrows/oldrows+1)[1:newrows] df[new.index,] } which just recycles the dataframe's rows enough times and cuts off the excess. Thanks again for the tip Kaspar -- Kaspar Pflugshaupt Geobotanisches Institut Zuerichbergstr. 38 CH-8044 Zuerich Tel. ++41 1 632 43 19 Fax ++41 1 632 12 15 mailto:pflugshaupt at geobot.umnw.ethz.ch privat:pflugshaupt at mails.ch http://www.geobot.umnw.ethz.ch -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._