Hello! Hope there is a nifty way to speed up my code by avoiding loops. My task is simple - analogous to the vlookup formula in Excel. Here is how I programmed it:
# My example data frame: set.seed(1245) my.df<-data.frame(names=rep(letters[1:3],3),value=round(rnorm(9,mean=20,sd=5),0)) my.df<-my.df[order(my.df$names),] my.df$names<-as.character(my.df$names) (my.df) # My example lookup table: my.lookup<-data.frame(names=letters[1:3],category=c("AAA","BBB","CCC")) my.lookup$names<-as.character(my.lookup$names) my.lookup$category<-as.character(my.lookup$category) (my.lookup) # Just adding an extra column to my.df that contains the categories of the names in the column "names": my.df2<-my.df my.df2$category<-NA for(i in unique(my.df$names)){ my.df2$category[my.df2$names %in% i]<-my.lookup$category[my.lookup$names %in% i] } (my.df2) It does what I need, but it's way too slow - I need to run it for hundreds and hundreds of names in >100 of huge files (tens of thousands of rows in each). Any way to speed it up? Thanks a lot! -- Dimitri Liakhovitski Ninah Consulting www.ninah.com ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.