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

David Wolfskill r at catwhisker.org
Fri Sep 11 18:54:16 CEST 2015


On Fri, Sep 11, 2015 at 04:42:07PM +0200, peter dalgaard wrote:
> Or change the data format slightly and use indexing:
> 
> > l
>      key    val    
> [1,] "1.1"  "10000"
> [2,] "1.9"  "10000"
> [3,] "1.4"  "15000"
> [4,] "1.5"  "20000"
> [5,] "1.15" "25000"
> > v <- l[,2]
> > names(v) <- l[,1]
> > x <- c("1.9", "1.9", "1.1", "1.1", "1.4", "1.4", "1.5", "1.5", "1.5", 
> + "1.5")
> > v[x]
>     1.9     1.9     1.1     1.1     1.4     1.4     1.5     1.5     1.5     1.5 
> "10000" "10000" "10000" "10000" "15000" "15000" "20000" "20000" "20000" "20000" 
> > 
> ...

Yeah --  *that* is actually what I was trying to accomplish; thank you
for the help!  (Not to detract from Bert's suggestion -- that also
works.  But Peter's is what I had been trying to accomplish before I
gave in & sent my plea.)

[This is actually a part of a function I'm cobbling up to filter
data.frames by values of a given column, where the acceptable bounds for
the values can vary depending on the contents of a different column.
E.g., we might expect certain "performance-related" metrics to have
different ranges of interest for different classes of hardware.]

And the function appears to be working so far (though I'll be poking and
prodding it a bit yet).

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/20150911/30b9e2f8/attachment.bin>


More information about the R-help mailing list