1

I want to merge three dataframes together, appending them as rows to the bottom of the previous, but I want their columns to match. Each dataframe has a different number of columns, but they share column names. EX:

Dataframe A                  Dataframe B                     Dataframe C
A B Y Z                      A B C X Y Z                     A B C D W X Y Z
# # # #                      # # # # # #                     # # # # # # # # 

In the end, I want them to look like:

Dataframe_Final
A B C D W X Y Z
# #         # #
# # #     # # #
# # # # # # # #

How can I merge these dataframes together in this way? Again, there's no ID for the rows that is unique (ascending, etc) across the dataframes. Thanks!

Captain Hat
  • 2,444
  • 1
  • 14
  • 31
PSB
  • 39
  • 7

1 Answers1

2

A base R option might be Reduce + merge

out <- Reduce(function(x,y) merge(x,y,all = TRUE),list(dfA,dfB,dfC))
out <- out[order(names(out))]

which gives

  A B  C  D  W  X Y Z
1 1 2 NA NA NA NA 3 4
2 1 2  3 NA NA  4 5 6
3 1 2  3  4  5  6 7 8

Dummy Data

dfA <- data.frame(A = 1, B = 2, Y = 3, Z = 4)
dfB <- data.frame(A = 1, B = 2, C = 3, X = 4, Y = 5, Z = 6)
dfC <- data.frame(A = 1, B = 2, C = 3, D = 4, W = 5, X = 6, Y = 7, Z = 8)
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81