21

My questions is how can join two or more data frames in system R?

For example:

I have two data frames:

first:

   x  y  z
1  3  2  4
2  4  5  7
3  5  6  8

second:

   x  y  z
1  1  1  1
2  4  5  7

I need this:

   x  y  z
1  3  2  4
2  4  5  7
3  5  6  8
4  1  1  1
5  4  5  7

I tried to use append for each vector, like this:

for( i in 1:length(first)){

    mix[[i]]<-append(first[i], second[i])}

f<-do.call(rbind, mix)

But It didn't work like I needed. I didn't get my matrix, i got some different structure.

Community
  • 1
  • 1
olga
  • 211
  • 1
  • 2
  • 4
  • Related questions : http://stackoverflow.com/questions/2851327/r-converting-a-list-of-data-frames-into-one-data-frame , http://stackoverflow.com/questions/2209258/merge-several-data-frames-into-one-data-frame-with-a-loop , http://stackoverflow.com/questions/1299871/how-to-join-data-frames-in-r-inner-outer-left-right , http://stackoverflow.com/questions/2392915/recombining-a-list-of-data-frames-into-a-single-data-frame , ... – Joris Meys Nov 10 '10 at 10:12

1 Answers1

32

You have the right idea using rbind(), but it's much more simple. If your data frames are named "first" and "second":

f <- rbind(first, second)

And f is the new data frame.

neilfws
  • 32,751
  • 5
  • 50
  • 63
  • Hey @neilfws, thanks, this helped me. Just dropping into the comments to say that the tone of your comment is so kind and encouraging, and thank you for that. – sleepy Aug 11 '20 at 06:06