[英]Join two data frames
I have two data frames: 我有两个数据框:
customer material Freq
1 1 12 1
2 1 10 1
3 1 8 1
4 1 4 1
5 1 3 1
6 1 2 1
and the second one: 第二个:
material class
1 12 A
2 10 B
3 3 C
4 4 D
5 5 E
6 6 F
Now I want to merge the two data frames. 现在我想合并两个数据帧。 I tried it with:
我尝试过:
e <- left_join(A, B, by="material")
But then I have all materials multiple times. 但后来我多次使用所有材料。 How can I solve this problem?
我怎么解决这个问题?
You could try: 你可以尝试:
left_join(A, B %>% distinct(), by="material")
Here I presume that you have some duplicate values in B
. 在这里,我假设你在
B
有一些重复的值。 With the data you provided the code runs fine; 使用您提供的数据,代码运行良好; I cannot see any material mentioned multiple times.
我看不到多次提到的任何材料。
Try this: You can reclass. 试试这个:你可以重新调整。
library(tidyverse)
#df1$material<-as.factor(df1$material)
#df2$material<-as.factor(df2$material)
#df2$class<-as.character(df2$class)
df1 %>%
left_join(df2,by="material")
If I understand the end result correctly then you could use base merge
: 如果我正确理解了最终结果,那么你可以使用base
merge
:
df3 <- merge(df1, df2, by = "material")
You can also use in pipes with : 您还可以在管道中使用:
df3 <- df1 %>%
merge(df2, by = "material")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.