所以我有两个数据框,我想将第一个数据框中的一列与另一个数据框中的第二列匹配。
df = data.frame(source=c("XRHxl8gq","2b790Qqv","mrgapJpQ","EsMfIbv1","ujOBob24","ujOBob24","EsMfIbv1"),
conv=c(362,247,222,160,86,65,34), all=c(19,17,26,12,22,25,11), intent=c(47,47,74,31,58,60,0))
df2 = data.frame(name=c("Bob","David","Mark","Sara","Alice","Cara","Chad","Donna","Elaine","Gary"),
source_id=c("XRHxl8gq","sr354136FH","2b790Qqv","myx645TH","mrgapJpQ","EsMfI546",
"ujOBob24","EsMfIbv1","fMHL45ts","sefihn"))
我想要结束的是将源与 source_id 匹配,以便我可以在 df 中插入一个带有名称的新列。
> df
source conv all intent who
1 XRHxl8gq 362 19 47 Bob
2 2b790Qqv 247 17 47 Mark
3 mrgapJpQ 222 26 74 Alice
4 EsMfIbv1 160 12 31 Cara
5 ujOBob24 86 22 58 Chad
6 ujOBob24 65 25 60 Chad
7 EsMfIbv1 34 11 0 Cara
# find what values in both columns are similar.
both = intersect(df[,1], df2[,2]) # IN BOTH COLUMNS
# create a new column in the original data frame.
df$who = c("")
# match up source with source_id.
str(df2)
df2$name = as.character(df2$name)
df$who[df$source %in% df2$source_id] <- df2$name
df
df$who[which(df$source %in% df2$source_id)]<-as.character(df2$name)
df
不幸的是,我似乎无法匹配列,以便每个源都与与每个 source_id 关联的名称匹配。
有人能帮忙吗?