2017-09-04 47 views
0

我试图用规则合并两个数据表,我无法将其转换为R代码。R:基于自定义规则合并数据表

让我们假设我们正在与客户打交道:每个客户可以属于一个或多个类别,并且每个类别都可以购买某个子集的产品。

然后我有两个数据帧进行合并,即

df1 
customer category 
Anthony X 
Anthony Y 
Beatrix Y 
Charles Z 

df2 
product category 
item1  X 
item2  Y 
item3  Y 
item3  Z 

df3 = required merge of (df1, df2) 
customer product 
Anthony item1 
Anthony item2 
Anthony item3 
Beatrix item2 
Beatrix item3 
Charles item3 

感谢您的帮助!

回答

1

根据您的示例,我理解为将与每个类别关联的所有产品都加入到每个客户的类别中。下面将在此情况下工作:

生成数据:

df1 <- read.table(header = T, text = "customer category 
Anthony X 
Anthony Y 
Beatrix Y 
Charles Z") 

df2 <- read.table(header = T, text = "product category 
item1  X 
item2  Y 
item3  Y 
item3  Z") 

dplyr包溶液:

library(dplyr) 
left_join(df1, df2) %>% select(-category) 

    customer product 
1 Anthony item1 
2 Anthony item2 
3 Anthony item3 
4 Beatrix item2 
5 Beatrix item3 
6 Charles item3 

编辑从基础包替代溶液(LMO建议的):

merge(df1, df2, by="category")[-1] 

    customer product 
1 Anthony item1 
2 Anthony item2 
3 Anthony item3 
4 Beatrix item2 
5 Beatrix item3 
6 Charles item3 
+1

你可能还需要添加基本的R方法:'merge(df1,df2,by =“类别“)[ - 1]'。 – lmo

+1

@lmo感谢您的建议。我已经合并了它。 :) –

+0

@ Z.Lin谢谢! – mac