Hello, Is there a simple way of stacking/merging two dataframes in R? I want to stack them piece-wise, not simply add one whole dataframe to the bottom of the other. I want to create as follows: x.frame: aX1 bX1 cX1 ... zX1 aX2 bX2 cX2 ... zX2 ... ... ... ... ... aX99 bX99 cX99 ... zX99 y.frame: aY1 bY1 cY1 ... zY1 aY2 bY2 cY2 ... zY2 ... ... ... ... ... aY99 bY99 cY99 ... zY99 new.frame: aX1 bX1 cX1 ... zX1 aY1 bY1 cY1 ... zY1 aX2 bX2 cX2 ... zX2 aY2 bY2 cY2 ... tY2 ... ... ... ... ... aX99 bX99 cX99 ... tX99 aY99 bY99 cY99 ... tY99 I have tried to use a for loop (simply assigning and also with rbind) to do this but am having difficulty correctly assigning the destination in the new dataframe. Can anyone offer a quick and easy way of doing this (or even a long winded one if it works!!) Thank you in advance, Laura Quinn Institute of Atmospheric Science School of the Environment University of Leeds Leeds LS2 9JT tel: +44 113 343 1596 fax: +44 113 343 6716 mail: laura at env.leeds.ac.uk
I believe interleave() in the `gregmisc' package can do what you want. Cheers, Andy> From: Laura Quinn > > Hello, > > Is there a simple way of stacking/merging two dataframes in > R? I want to > stack them piece-wise, not simply add one whole dataframe to > the bottom of > the other. I want to create as follows: > > x.frame: > aX1 bX1 cX1 ... zX1 > aX2 bX2 cX2 ... zX2 > ... ... ... ... ... > aX99 bX99 cX99 ... zX99 > > y.frame: > aY1 bY1 cY1 ... zY1 > aY2 bY2 cY2 ... zY2 > ... ... ... ... ... > aY99 bY99 cY99 ... zY99 > > new.frame: > aX1 bX1 cX1 ... zX1 > aY1 bY1 cY1 ... zY1 > aX2 bX2 cX2 ... zX2 > aY2 bY2 cY2 ... tY2 > ... ... ... ... ... > aX99 bX99 cX99 ... tX99 > aY99 bY99 cY99 ... tY99 > > I have tried to use a for loop (simply assigning and also > with rbind) to > do this but am having difficulty correctly assigning the > destination in the new dataframe. Can > anyone offer a quick and easy way of doing this (or even a > long winded one > if it works!!) > > Thank you in advance, > > Laura Quinn > Institute of Atmospheric Science > School of the Environment > University of Leeds > Leeds > LS2 9JT > > tel: +44 113 343 1596 > fax: +44 113 343 6716 > mail: laura at env.leeds.ac.uk > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
or something like new.frame<-rbind(x.frame,y.frame); # A frame of the right size. new.frame[seq(1,nrow(x.frame),by=2),] <- x.frame # Assign every other row new.frame[seq(2,nrow(x.frame),by=2),] <- y.frame # Assign every other row. -----Original Message----- From: Laura Quinn [mailto:laura at env.leeds.ac.uk] Sent: Sunday, August 15, 2004 7:11 PM To: r-help at stat.math.ethz.ch Subject: [R] Stacking Vectors/Dataframes Hello, Is there a simple way of stacking/merging two dataframes in R? I want to stack them piece-wise, not simply add one whole dataframe to the bottom of the other. I want to create as follows: x.frame: aX1 bX1 cX1 ... zX1 aX2 bX2 cX2 ... zX2 ... ... ... ... ... aX99 bX99 cX99 ... zX99 y.frame: aY1 bY1 cY1 ... zY1 aY2 bY2 cY2 ... zY2 ... ... ... ... ... aY99 bY99 cY99 ... zY99 new.frame: aX1 bX1 cX1 ... zX1 aY1 bY1 cY1 ... zY1 aX2 bX2 cX2 ... zX2 aY2 bY2 cY2 ... tY2 ... ... ... ... ... aX99 bX99 cX99 ... tX99 aY99 bY99 cY99 ... tY99 I have tried to use a for loop (simply assigning and also with rbind) to do this but am having difficulty correctly assigning the destination in the new dataframe. Can anyone offer a quick and easy way of doing this (or even a long winded one if it works!!) Thank you in advance, Laura Quinn Institute of Atmospheric Science School of the Environment University of Leeds Leeds LS2 9JT tel: +44 113 343 1596 fax: +44 113 343 6716 mail: laura at env.leeds.ac.uk ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Oops there was a bug... new.frame<-rbind(x.frame,y.frame); # A frame of the right size. new.frame[seq(1,nrow(new.frame),by=2),] <- x.frame # Assign every other row new.frame[seq(2,nrow(new.frame),by=2),] <- y.frame # Assign every other row. -----Original Message----- From: Samuelson, Frank* Sent: Monday, August 16, 2004 9:55 AM To: 'r-help at stat.math.ethz.ch' Subject: Re: [R] Stacking Vectors/Dataframes or something like new.frame<-rbind(x.frame,y.frame); # A frame of the right size. new.frame[seq(1,nrow(x.frame),by=2),] <- x.frame # Assign every other row new.frame[seq(2,nrow(x.frame),by=2),] <- y.frame # Assign every other row. -----Original Message----- From: Laura Quinn [mailto:laura at env.leeds.ac.uk] Sent: Sunday, August 15, 2004 7:11 PM To: r-help at stat.math.ethz.ch Subject: [R] Stacking Vectors/Dataframes Hello, Is there a simple way of stacking/merging two dataframes in R? I want to stack them piece-wise, not simply add one whole dataframe to the bottom of the other. I want to create as follows: x.frame: aX1 bX1 cX1 ... zX1 aX2 bX2 cX2 ... zX2 ... ... ... ... ... aX99 bX99 cX99 ... zX99 y.frame: aY1 bY1 cY1 ... zY1 aY2 bY2 cY2 ... zY2 ... ... ... ... ... aY99 bY99 cY99 ... zY99 new.frame: aX1 bX1 cX1 ... zX1 aY1 bY1 cY1 ... zY1 aX2 bX2 cX2 ... zX2 aY2 bY2 cY2 ... tY2 ... ... ... ... ... aX99 bX99 cX99 ... tX99 aY99 bY99 cY99 ... tY99 I have tried to use a for loop (simply assigning and also with rbind) to do this but am having difficulty correctly assigning the destination in the new dataframe. Can anyone offer a quick and easy way of doing this (or even a long winded one if it works!!) Thank you in advance, Laura Quinn Institute of Atmospheric Science School of the Environment University of Leeds Leeds LS2 9JT tel: +44 113 343 1596 fax: +44 113 343 6716 mail: laura at env.leeds.ac.uk ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html