[R] appending similar data frames?

Michael A. Miller mmiller3 at iupui.edu
Mon Dec 3 20:35:54 CET 2001


Dear R gang,

Can anyone help me sort out how to append one data frame to
another while adding a factor to distinguish which was the
original frame?

For example, I have two frames, x and y

> x
  exp size
1   a   10
2   b    9
3   c   10
4   d   12
5   e   11
 
> y 
  exp size
1   a   13
2   b   15
3   c   12
4   d   20
5   e   15

and I'd like to create a new frame that looks like

   exp size set
1    a   10   x
2    b    9   x
3    c   10   x
4    d   12   x
5    e   11   x
6    a   13   y
7    b   15   y
8    c   12   y
9    d   20   y
10   e   15   y

I know that I can do something like

> new.frame <- data.frame(c(as.vector(x$exp), as.vector(y$exp)))
> new.frame$size <- c(as.vector(x$size), as.vector(y$size))
> new.frame$set <- c(rep('x',times=length(x$exp)), rep('y',times=length(y$exp)))
> names(new.frame) <- c('exp','size','set')

Is there any generalized code out there that will do this sort of
thing for more complex data frames (still with the same structure
though)?  Or is there a simpler R idiom that does the same thing?

Mike
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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