[R] dictionary lookup

Thomas Manke manke at molgen.mpg.de
Fri Mar 7 00:45:19 CET 2008


Hi,

I have a character-valued vector (old_names) and want to translate
its entries whenever possible,  using a dictionary (dict=data.frame).
The translation direction is dict$V3 --> dict$V2, but
some values may be undefined (NA). I suppose this is a very basic
task, but I tried in vain to make it more efficient than below.
In particular I would like to avoid the explicit (and slow) loop
Any help is very much welcome.
Thank you, TM
============================================
new_names = old_names
m = match(old_names, dict$V3)
N = length(old_names)
for (i in 1:N) {
     if (is.na(m[i])) { next ; }

     nn = as.vector(dict$V2)[m[i]];
    if (nn == "" ) { next; }

    new_names[i] = nn
}



More information about the R-help mailing list