So I have two data frame, and I want to match up one column in the first data frame with a second column in the other data frame.
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"))
What I want to end up with is match source with source_id so that I can insert a new column in df with the name.
> 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
Unfortunately, I can’t seem to match up the columns such that each source is matched up with the name associated with each source_id.
Can anyone help with this?
You’re looking for
merge: