Merging Two Data Frames

Hello, I have two different data frames with the exact same column names for each data frame. For example, one data frame has data for 2012. The other has data for 2013. Now, remember that each data frame has the same number of columns and same type of columns, let's say 4 columns, for both data frames. How do I add all the data for 2013 to the end of 2012, to make just 1 combined data set for 2012-2013 with the same type of columns (4 columns still)?


Take a look at the function purrr::map_dfr and/or dplyr::bind_rows. In your case I suspect that bind_rows does what you need.

Any way to do this using tidyverse commands?

Both dplyr and purrr are very much part of tidyverse.

1 Like

Since you said each dataframe has the same columns, bind rows is easy

If you have tidyverse loaded, you have dplyr loaded. I would set an extra column with year as the value first. bind_rows does have an .id argument, but I prefer having my data setup exactly how I want ahead of time. You would use mutate (another tidyverse verb) to do that. For example in dataset one mutate (year = 2012) and in dataset 2 mutate (year =2013). Im assuming you already have the datasets in a pipeline when you call mutate. Sorry for the poor formatting. Iā€™m on my phone.


I think, it's a little misleading to call anything tidyverse verb. tidyverse itself is not a package in a full sense of the word since there are no exported functions. So it is better to think of each individual package as being responsible for certain part of the workflow (such as dplyr being responsible for mutate and bind_rows). This way there is no misunderstanding about, for example, where to look for help.

That is a good point. I was trying to make it clear that it was part of the tidyverse, since he felt that it as important to stay in the tidyverse. Though simply commenting that they are part of the dplyr package, which is in turn part of the tidyverse would have been more clear.