[R] How to create a data.frame "like" another, but longer?

Ott Toomet siim at obs.ee
Wed Dec 19 15:22:09 CET 2001


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( ...,

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

More information about the R-help mailing list