Reputation: 133
I have several R data frames. There are some columns with common column names, but there are also some columns with differing names in each data frames. I would like to combine rows of these data frames, so that the columns with the same names are added with new rows, and the columns with the differing column names can be kept in the new data frame. A replicable example would look something like this:
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(2:11),
speed = c(2, 3, 4, 5, 6, 7, 8, 9, 2, 3))
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 speed
1 2 2
2 3 3
3 4 4
4 5 5
5 6 6
6 7 7
7 8 8
8 9 9
9 10 2
10 11 3
The desired output would look like this:
Obs Color Weight speed
1 1 red 1.1
2 2 red 1.2
3 3 red 1.3
4 4 green 1.4
5 2 2
6 3 3
7 4 4
8 5 5
9 6 6
10 7 7
11 8 8
12 9 9
13 10 2
14 11 3
Thank you very much.
Upvotes: 0
Views: 655
Reputation: 10996
library(tidyverse)
bind_rows(apple, orange)
which gives:
Obs Color Weight speed
1 1 red 1.1 NA
2 2 red 1.2 NA
3 3 red 1.3 NA
4 4 green 1.4 NA
5 2 <NA> NA 2
6 3 <NA> NA 3
7 4 <NA> NA 4
8 5 <NA> NA 5
9 6 <NA> NA 6
10 7 <NA> NA 7
11 8 <NA> NA 8
12 9 <NA> NA 9
13 10 <NA> NA 2
14 11 <NA> NA 3
Upvotes: 1
Reputation: 2783
The package plyr
has a useful function for this:
library(plyr)
rbind.fill(apple, orange)
Giving the following table:
Obs Color Weight speed
1 1 red 1.1 NA
2 2 red 1.2 NA
3 3 red 1.3 NA
4 4 green 1.4 NA
5 2 <NA> NA 2
6 3 <NA> NA 3
7 4 <NA> NA 4
8 5 <NA> NA 5
9 6 <NA> NA 6
10 7 <NA> NA 7
11 8 <NA> NA 8
12 9 <NA> NA 9
13 10 <NA> NA 2
14 11 <NA> NA 3
Upvotes: 0