3
apple = data.frame(Obs = c(1:4), Color = c("red", "red", "red", "green"), Weight = c(1.1, 1.2, 1.3, 1.4))
orange = data.frame(Obs = c(1:6), Weight = c(2, 3, 4, 5, 6, 7))

I have two data.frames, apple and orange, in which the latter's columns are a subset of the former's.

> apple
  Obs Color Weight
1   1   red    1.1
2   2   red    1.2
3   3   red    1.3
4   4 green    1.4
> orange
  Obs Weight
1   1      2
2   2      3
3   3      4
4   4      5
5   5      6
6   6      7

I would like to merge the 2 data.frames and the result should look something like this:

> apple_orange
      Obs Color Weight
    1   1   red    1.1
    2   2   red    1.2
    3   3   red    1.3
    4   4 green    1.4
    5   1    NA     2
    6   2    NA     3
    7   3    NA     4
    8   4    NA     5
    9   5    NA     6
    10  6    NA     7

What's a way to merge this so that I do not have the specify the specific column names? I.e. my actual dataset has hundreds of columns in common, so I'd rather not type them out one by one.

Adrian
  • 9,229
  • 24
  • 74
  • 132

1 Answers1

8

You can use dplyr::bind_rows which matches columns by name and fill missing columns with NA, here is the docs:

When row-binding, columns are matched by name, and any values that don't match will be filled with NA.

dplyr::bind_rows(apple, orange)

   Obs Color Weight
1    1   red    1.1
2    2   red    1.2
3    3   red    1.3
4    4 green    1.4
5    1  <NA>    2.0
6    2  <NA>    3.0
7    3  <NA>    4.0
8    4  <NA>    5.0
9    5  <NA>    6.0
10   6  <NA>    7.0
Psidom
  • 209,562
  • 33
  • 339
  • 356