'Merge 2 datasets in directly below one another

I have two datasets:

y:

Country month New_Users Returning_Users line
Angola 2020-05-01 4 5 clientline
Angola 2020-06-01 5 6 clientline
Ghana 2020-06-01 10 15 clientline

x:

Country month New_Users Returning_Users line
Angola 2020-05-01 1 8 teamline
Angola 2020-06-01 3 10 teamline
Ghana 2020-06-01 5 2 teamline

I want to merge them to return:

Country month New_Users Returning_Users line
Angola 2020-05-01 4 5 clientline
Angola 2020-05-01 1 8 teamline
Angola 2020-06-01 5 6 clientline
Angola 2020-06-01 3 10 teamline
Ghana 2020-06-01 10 15 clientline
Ghana 2020-06-01 5 2 teamline

So it should merge by both country and line and y dataset should go below x dataset. Many of the merging methods I'm familiar with paste the data frames side by side instead of below one another.

Please help



Solution 1:[1]

You can just use rbind like this:

y <- data.frame(Country = c('Angola', 'Angola', 'Ghana'),
                month = c("2020-05-01", "2020-06-01", "2020-06-01"),
                New_Users = c(4,5,10),
                Returning_Users = c(5,6,15),
                line = c("clientline", "clientline", "clientline"))

x <- data.frame(Country = c('Angola', 'Angola', 'Ghana'),
                month = c("2020-05-01", "2020-06-01", "2020-06-01"),
                New_Users = c(1,3,5),
                Returning_Users = c(8,10,12),
                line = c("teamline", "teamline", "teamline"))

rbind(y,x)

Output:

  Country      month New_Users Returning_Users       line
1  Angola 2020-05-01         4               5 clientline
2  Angola 2020-06-01         5               6 clientline
3   Ghana 2020-06-01        10              15 clientline
4  Angola 2020-05-01         1               8   teamline
5  Angola 2020-06-01         3              10   teamline
6   Ghana 2020-06-01         5              12   teamline

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Quinten