[R] lookups and joins
Paul Sorenson
Paul.Sorenson at vision-bio.com
Mon Jan 24 23:34:59 CET 2005
I have some data coming from SQL sources that I wish to relate in various ways. For reasons only known to our IT people, this can't be done in SQL at present.
I am looking for an R'ish technique for looking up new columns on a data frame. As a simple, hardwired example I have tried the following:
# This gives me two columns, one the lookup value and the second one
# the result column, ie my lookup table.
stcl = read.csv("stockclass.csv")
stockclass = as.vector(stcl$stock_class)
# This gives me what appears to be a dictionary or map
names(stockclass) = as.vector(stcl$stock_group)
getstockclass = function(stock_group) {
try(stockclass[[stock_group]], TRUE)
}
csg$stk_class=factor(sapply(csg$stock_group, getstockclass))
I need the try since if there is a missing value I get an exception.
I also tried something along the lines of (from memory):
getstockclass = function(stock_group) {
stcl[which(stcl$stock_group == stock_group),]$stock_class
}
These work but I just wanted to check if there was an inbuilt way to do this kind of thing in R? I searched on "join" without much luck.
Really what I would like is a generic function that:
- Takes 2 data frames,
- Some kind of specification on which column(s) to join
- Outputs the joined frames, or perhaps a vector which is an index vector that I can use on the second data frame.
I don't really want to reinvent SQL and my data sets are not huge.
cheers
More information about the R-help
mailing list