r - Comparing columns in two different data frame -



r - Comparing columns in two different data frame -

so have 2 info frame, , want match 1 column in first info frame sec column in other info 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 want end match source source_id can insert new column in df name.

> df source conv intent 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 republic of chad 6 ujobob24 65 25 60 republic of chad 7 esmfibv1 34 11 0 cara # find values in both columns similar. both = intersect(df[,1], df2[,2]) # in both columns # create new column in original info frame. df$who = c("") # match source 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, can't seem match columns such each source matched name associated each source_id.

can help this?

you're looking merge:

merge(df,df2, by.x="source", by.y="source_id", sort=f) # source conv intent name # 1 xrhxl8gq 362 19 47 bob # 2 2b790qqv 247 17 47 mark # 3 mrgapjpq 222 26 74 alice # 4 esmfibv1 160 12 31 donna # 5 esmfibv1 34 11 0 donna # 6 ujobob24 86 22 58 republic of chad # 7 ujobob24 65 25 60 republic of chad

r

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -