[R] Generate a vector of values, given a vector of keys and a table?

David Wolfskill r at catwhisker.org
Fri Sep 11 07:04:29 CEST 2015


I apologize in advance: I must be overlooking something quite simple,
but I'm failing to make progress.

Suppose I have a "lookup table":

Browse[2]> dput(lk_up)
structure(c("1.1", "1.9", "1.4", "1.5", "1.15", "10000", "10000", 
"15000", "20000", "25000"), .Dim = c(5L, 2L), .Dimnames = list(
    NULL, c("key", "val")))
Browse[2]> lk_up
     key    val    
[1,] "1.1"  "10000"
[2,] "1.9"  "10000"
[3,] "1.4"  "15000"
[4,] "1.5"  "20000"
[5,] "1.15" "25000"

and a vector whose elements correspond with the "key" column of the
table:

Browse[2]> dput(x)
c("1.9", "1.9", "1.1", "1.1", "1.4", "1.4", "1.5", "1.5", "1.5", 
"1.5")
Browse[2]> x
 [1] "1.9" "1.9" "1.1" "1.1" "1.4" "1.4" "1.5" "1.5" "1.5" "1.5"
Browse[2]> 

Is there a (relatively) simple (i.e., not explicitly looping) construct
that will yield a vector of the same size and shape as "x", but contain
the "value" entries from the lookup table (preserving the sequence: the
1st entry of the result must correspond to the 1st entry of the list of
keys) -- in the current example:

Browse[2]> dput(y)
c("10000", "10000", "10000", "10000", "15000", "15000", "20000", 
"20000", "20000", "20000")
Browse[2]> y
 [1] "10000" "10000" "10000" "10000" "15000" "15000" "20000" "20000"
"20000" "20000"
Browse[2]> 

I am (unfortunately) presently limited to R-3.0.2.

Thanks....

Peace,
david
-- 
David H. Wolfskill				r at catwhisker.org
Those who would murder in the name of God or prophet are blasphemous cowards.

See http://www.catwhisker.org/~david/publickey.gpg for my public key.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 949 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20150910/ca3c75f8/attachment.bin>


More information about the R-help mailing list