0

I would like to match 2 columns in 2 dataframes. For example: Data frame 1:

df1 <- data.frame(CODE.1=c(66, 75, 87, 134))

Data frame 2:

df2 <- data.frame(CODE.2=c(75, 134, 83, 2))

Output I'd like to get:

df3 <- data.frame(CODE.1=c(66, 75, 87, 134, "NA", "NA"),
                  CODE.2=c("NA", 75, "NA", 134, 2, 83))

Thanks for your help

Jaap
  • 81,064
  • 34
  • 182
  • 193
user3262756
  • 649
  • 1
  • 9
  • 27
  • Surely you could have found the question by searching! E.g. [this one](http://stackoverflow.com/questions/16962576/how-can-i-rbind-vectors-matching-their-column-names). From there, there's a one-line solution using `gtools`: `smartbind(df1$CODE.1, df2$CODE.2)` – Rasmus Oct 26 '14 at 23:51
  • I tried searching but with no success. Many thanks for the code. – user3262756 Oct 27 '14 at 18:51

1 Answers1

0

You could use merge in a somewhat unconventional way:

df3 <- merge(transform(df1, CODE = CODE.1),
             transform(df2, CODE = CODE.2),
             all = TRUE)

#   CODE CODE.1 CODE.2
# 1    2     NA      2
# 2   66     66     NA
# 3   75     75     75
# 4   83     NA     83
# 5   87     87     NA
# 6  134    134    134
flodel
  • 87,577
  • 21
  • 185
  • 223