Home > Back-end >  How to Bind Two Data Fames with Multiple Columns based on the Column Name in R
How to Bind Two Data Fames with Multiple Columns based on the Column Name in R

Time:10-18

I am trying to bind two data frames which has more than 600 column and each of those 600 columns are unique names.

I want to add the two data frame one below the other based on the column names, I can do it when there are a few columns but I am not sure how to about with it with more than 600 columns as writing the 600 columns names for merge is quite difficult. Can someone help me out?

Attached is the simplified data of both data frame.

This is one of the data frame called pd:

structure(list(ds = c("2019-01-01", "2019-02-01", "2019-03-01", 
"2019-04-01", "2019-05-01", "2019-06-01", "2019-07-01", "2019-08-01", 
"2019-09-01", "2019-10-01", "2019-11-01", "2019-12-01", "2020-01-01", 
"2020-02-01", "2020-03-01", "2020-04-01", "2020-05-01", "2020-06-01", 
"2020-07-01", "2020-08-01", "2020-09-01", "2020-10-01", "2020-11-01", 
"2020-12-01", "2021-01-01", "2021-02-01", "2021-03-01", "2021-04-01", 
"2021-05-01", "2021-06-01", "2021-07-01", "2021-08-01", "2021-09-01", 
"2021-10-01", "2021-11-01", "2021-12-01", "2022-01-01", "2022-02-01", 
"2022-03-01", "2022-04-01", "2022-05-01", "2022-06-01", "2022-07-01", 
"2022-08-01"), X1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 
85, 72, 111, 96, 50, 95, 48, 87, 75, 249, 173, 74, 86, 127, 209, 
92, 137, 49, 84, 75, 73, 376, 196, 91, 107, 124, 177, 244, 275, 
100, 176), X2 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19, 29, 243, 
281, 262, 283, 0, 264, 104, 289, 41, 76), X3 = c(0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 157, 171, 377, 409, 375, 314, 253, 322, 
130, 472, 115, 179)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -44L))

enter image description here

This is the other data frame, pd1

structure(list(ds = structure(c(19236, 19266, 19297, 19327, 19358, 
19389, 19417, 19448, 19478), class = "Date"), X1 = structure(c(103.045278486668, 
103.045278486668, 103.045278486668, 103.045278486668, 103.045278486668, 
103.045278486668, 103.045278486668, 103.045278486668, 103.045278486668
), tsp = c(2022.66666666667, 2023.33333333333, 12), class = "ts"), 
    X2 = structure(c(9.97152706820806, 9.97152706820806, 9.97152706820806, 
    9.97152706820806, 9.97152706820806, 9.97152706820806, 9.97152706820806, 
    9.97152706820806, 9.97152706820806), tsp = c(2022.66666666667, 
    2023.33333333333, 12), class = "ts"), X3 = structure(c(21.2001463509872, 
    21.2001463509872, 21.2001463509872, 21.2001463509872, 21.2001463509872, 
    21.2001463509872, 21.2001463509872, 21.2001463509872, 21.2001463509872
    ), tsp = c(2022.66666666667, 2023.33333333333, 12), class = "ts")), class = "data.frame", row.names = c(NA, 
-9L))

enter image description here

CodePudding user response:

If they have a similar structure, you can simply use rbind?

rbind(pd, pd1)

Courtesy of @jay.sf in the comments below: The rbind function will automatically combine the columns with corresponding names.

  • Related