[R] lookup in R - possible to avoid loops?

Dimitri Liakhovitski
2010-11-08T19:43:30Z

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