0

I want to combine several data frames which contains some common variable names as well as extra variables.

A reproducible example is as follow:

df1 <- data.frame(a=1:5, b=1:5)
df2 <- data.frame(a=1:5, b=6:10, c=11:15)

I would like to merge df1 and df2 as follow:

 a  b  c
1  1  1 NA
2  2  2 NA
3  3  3 NA
4  4  4 NA
5  5  5 NA
6  1  6 11
7  2  7 12
8  3  8 13
9  4  9 14
10 5 10 15
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

1

You can also try the following (apart from any of the answers mentioned in the comments!) -

df1 <- data.frame(a=1:5, b=1:5)
df2 <- data.frame(a=1:5, b=6:10, c=11:15)    
data.table::rbindlist(list(df1, df2), use.names = TRUE, fill = TRUE)
 a  b  c
 1: 1  1 NA
 2: 2  2 NA
 3: 3  3 NA
 4: 4  4 NA
 5: 5  5 NA
 6: 1  6 11
 7: 2  7 12
 8: 3  8 13
 9: 4  9 14
10: 5 10 15
Ameya
  • 1,712
  • 1
  • 14
  • 29