David Wolfskill
2015-Sep-11 05:04 UTC
[R] Generate a vector of values, given a vector of keys and a table?
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>
Bert Gunter
2015-Sep-11 05:30 UTC
[R] Generate a vector of values, given a vector of keys and a table?
?match as in:> y <- lk_up[match(x,lk_up[,"key"]),"val"] > y[1] "10000" "10000" "10000" "10000" "15000" "15000" "20000" [8] "20000" "20000" "20000" Bert Bert Gunter "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." -- Clifford Stoll On Thu, Sep 10, 2015 at 10:04 PM, David Wolfskill <r at catwhisker.org> wrote:> 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. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
David Wolfskill
2015-Sep-11 11:23 UTC
[R] Generate a vector of values, given a vector of keys and a table?
On Thu, Sep 10, 2015 at 10:30:50PM -0700, Bert Gunter wrote:> ?match > > as in: > > > y <- lk_up[match(x,lk_up[,"key"]),"val"] > > y > [1] "10000" "10000" "10000" "10000" "15000" "15000" "20000" > [8] "20000" "20000" "20000" > ...Aye -- thank you very much! 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/9ad0700f/attachment.bin>
peter dalgaard
2015-Sep-11 14:42 UTC
[R] Generate a vector of values, given a vector of keys and a table?
Or change the data format slightly and use indexing:> lkey 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">On 11 Sep 2015, at 07:30 , Bert Gunter <bgunter.4567 at gmail.com> wrote:> ?match > > as in: > >> y <- lk_up[match(x,lk_up[,"key"]),"val"] >> y > [1] "10000" "10000" "10000" "10000" "15000" "15000" "20000" > [8] "20000" "20000" "20000" > > > > Bert > > > > Bert Gunter > > "Data is not information. Information is not knowledge. And knowledge > is certainly not wisdom." > -- Clifford Stoll > > > On Thu, Sep 10, 2015 at 10:04 PM, David Wolfskill <r at catwhisker.org> wrote: >> 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. >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com