基于两个数据集之间共享列的行绑定

时间:2018-10-24 20:46:28

标签: r

给出两个具有一定数量共享列的数据框,如何通过自动查找共同共享的列并将一个附加到另一列的方式将数据框连接在一起(自动为不跨数据框共享的列填充NA)< / p>

例如,如果我有这个

 df1 = data.frame(CustomerId = c(1:6), Product = c(rep("Toaster", 3), rep("Radio", 3)))
 df2 = data.frame(CustomerId = c(2, 4, 6), State = c(rep("Alabama", 2), rep("Ohio", 1)))

df1
#  CustomerId Product
#           1 Toaster
#           2 Toaster
#           3 Toaster
#           4   Radio
#           5   Radio
#           6   Radio

df2
#  CustomerId   State
#           7 Alabama
#           8 Alabama
#           9    Ohio

magicFunction(df1,df2)将返回这样的数据帧:

final_df
df1
#  CustomerId Product State
#           1 Toaster NA
#           2 Toaster NA
#           3 Toaster NA
#           4   Radio NA
#           5   Radio NA
#           6   Radio NA
#           7 NA      Alabama
#           8 NA      Alabama
#           9 NA      Ohio

我尝试了dplyr的不同join品种,但无法弄清楚。

1 个答案:

答案 0 :(得分:2)

bind_rows应该可以解决您的问题

library(tidyverse)
df1 = data.frame(CustomerId = c(1:6), Product = c(rep("Toaster", 3), rep("Radio", 3)))
df2 = data.frame(CustomerId = c(2, 4, 6), State = c(rep("Alabama", 2), rep("Ohio", 1)))

new_df <- bind_rows(df1, df2)