Re: [R] Add column to DF based on 2 columns in another DF

2013-12-10 Thread bcrombie
Thanks, AK. That gets the job done and I learned 2 ways to do it. Much appreciated as always. -- View this message in context: http://r.789695.n4.nabble.com/Add-column-to-DF-based-on-2-columns-in-another-DF-tp4681950p4681969.html Sent from the R help mailing list archive at Nabble.com. ___

[R] Add column to DF based on 2 columns in another DF

2013-12-10 Thread bcrombie
LookupTable <- read.table(header = TRUE, stringsAsFactors = FALSE, text="Str IND13 IND12 IND11 IND07 IND06 1 517 529 562 562 567 2 517 529 562 562 568 3 517 529

Re: [R] Add column to DF based on 2 columns in another DF

2013-12-10 Thread arun
Hi, Try: library(reshape2) m1 <- melt(LookupTable,id.vars="Str") m2 <- m1 res <- merge(MainDataFrame,m1,by.x=c("Str","index"),by.y=c("Str","variable")) res[order(res$Str),c(3:6,1:2,7)] #or library(plyr) colnames(m2)[-1] <- c("index","index_num")  m2$index <- as.character(m2$index)  join(MainDataF