damien
damien

Reputation: 69

Combining data sets vertically

Suppose I have three datasets

DF1=
Traj X Y
1   4 5
1   7 8
2   5 6
DF2=

Traj X Y
1   5 2
1   6 4
2   8 7
DF3=

Traj X Y
1   8 5
1   1 9
2   3 7

How can I combine them into one dataset like below:

DF1=

Traj X Y
1   4 5
1   7 8
2   5 6
1   5 2
1   6 4
2   8 7
1   8 5
1   1 9
2   3 7

I have many similar data that have similar elements and it would be easier to just combine them, unfortunately I don't know how to do so.

Upvotes: 2

Views: 91

Answers (4)

Ben Bolker
Ben Bolker

Reputation: 226152

In base R, rbind(DF1, DF2, DF3 (rbind stands for "row bind", i.e. "put rows together"). In tidyverse, dplyr::bind_rows(DF1, DF2, DF3) is slightly fancier.

If you already have a list of your data frames,

DFlist <- list(DF1, DF2, DF3)
do.call(DFlist, rbind)

Upvotes: 3

TarJae
TarJae

Reputation: 78927

This works also:

bind_rows(DF1, DF2, DF3)

Upvotes: 2

akrun
akrun

Reputation: 887048

Another option is to place it in a list and use bind_rows

library(dplyr)
mget(ls(pattern = '^DF\\d+$')) %>%
       bind_rows

Upvotes: 2

AnilGoyal
AnilGoyal

Reputation: 26218

Simply do this

rbind(df1, df2, df3) 

Upvotes: 2

Related Questions