[R] Using Match in a lookup table
Hi All, I'm having a few problems using match and a lookup table, previous Googling show numerous solutions to matching a lookup table to a dataset, My situation is slightly different as i have multiple lookup tables, (that i cannot merge - for integrity reasons) that i wish to match against my data, and each of these files is large, so lots of for / if conditions are not ideal. (withstanding that i have multiple files of course) For example, I have data: v - c(foo, foo, bar, bar, baz) id - c(1,2) id2 - c(3) name - c(foo, bar) name2 - c(baz) df1 - data.frame(id, name) df2 - data.frame(id2, name2) v - df1$id[match(v,df1$name)] v [1] 1 1 2 2 NA So here i actually want to return v [1] 1 1 2 2 baz so next time i can run v - df2$id[match(v,df2$name)] and return: v [1] 1 1 2 2 3 Any help very much appreciated Mike [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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.
Re: [R] Using Match in a lookup table
On Jun 28, 2011, at 6:18 AM, Michael Pearmain wrote: Hi All, I'm having a few problems using match and a lookup table, previous Googling show numerous solutions to matching a lookup table to a dataset, My situation is slightly different as i have multiple lookup tables, (that i cannot merge - for integrity reasons) that i wish to match against my data, and each of these files is large, so lots of for / if conditions are not ideal. (withstanding that i have multiple files of course) For example, I have data: v - c(foo, foo, bar, bar, baz) id - c(1,2) id2 - c(3) name - c(foo, bar) name2 - c(baz) df1 - data.frame(id, name) df2 - data.frame(id2, name2) v - df1$id[match(v,df1$name)] v [1] 1 1 2 2 NA A numeric vector. So here i actually want to return v [1] 1 1 2 2 baz Not possibly a numeric vector. so next time i can run v - df2$id[match(v,df2$name)] and return: v [1] 1 1 2 2 3 You need to keep track of the successful matches in df1 and then ypu probably want to interleave them with matches in df2. Perhaps instead use ifelse to do the work for you: ifelse(!is.na(match(v,df1$name)), match(v,df1$name), match(v,df2$name2) ) [1] 1 1 2 2 1 Any help very much appreciated Mike [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 Winsemius, MD West Hartford, CT __ R-help@r-project.org mailing list 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.
Re: [R] Using Match in a lookup table
On Jun 28, 2011, at 10:29 AM, Michael Pearmain wrote: Thanks for the idea David, My problem comes from having (say) upto 10 different match files, so nested ifelse whilst would work doesn't seem and elegant solution, Well, you cannot mix text and numeric modes in a vector as you appeared to expect. There may be a limit on the nesting if ifelse's as well. ISTR 7 but have not been able to document that vague memory. It seems that separate matching runs followed by some logic that cbind()-ed and then collapsed the results perhaps by just dropping all the NA's row-wise. -- David. However if needs must.. Mike On 28 June 2011 14:39, David Winsemius dwinsem...@comcast.net wrote: On Jun 28, 2011, at 6:18 AM, Michael Pearmain wrote: Hi All, I'm having a few problems using match and a lookup table, previous Googling show numerous solutions to matching a lookup table to a dataset, My situation is slightly different as i have multiple lookup tables, (that i cannot merge - for integrity reasons) that i wish to match against my data, and each of these files is large, so lots of for / if conditions are not ideal. (withstanding that i have multiple files of course) For example, I have data: v - c(foo, foo, bar, bar, baz) id - c(1,2) id2 - c(3) name - c(foo, bar) name2 - c(baz) df1 - data.frame(id, name) df2 - data.frame(id2, name2) v - df1$id[match(v,df1$name)] v [1] 1 1 2 2 NA A numeric vector. So here i actually want to return v [1] 1 1 2 2 baz Not possibly a numeric vector. so next time i can run v - df2$id[match(v,df2$name)] and return: v [1] 1 1 2 2 3 You need to keep track of the successful matches in df1 and then ypu probably want to interleave them with matches in df2. Perhaps instead use ifelse to do the work for you: ifelse(!is.na(match(v,df1$name)), match(v,df1$name), match(v,df2$name2) ) [1] 1 1 2 2 1 Any help very much appreciated Mike [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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 Winsemius, MD West Hartford, CT David Winsemius, MD West Hartford, CT [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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.
Re: [R] Using Match in a lookup table
Thanks for the idea David, My problem comes from having (say) upto 10 different match files, so nested ifelse whilst would work doesn't seem and elegant solution, However if needs must.. Mike On 28 June 2011 14:39, David Winsemius dwinsem...@comcast.net wrote: On Jun 28, 2011, at 6:18 AM, Michael Pearmain wrote: Hi All, I'm having a few problems using match and a lookup table, previous Googling show numerous solutions to matching a lookup table to a dataset, My situation is slightly different as i have multiple lookup tables, (that i cannot merge - for integrity reasons) that i wish to match against my data, and each of these files is large, so lots of for / if conditions are not ideal. (withstanding that i have multiple files of course) For example, I have data: v - c(foo, foo, bar, bar, baz) id - c(1,2) id2 - c(3) name - c(foo, bar) name2 - c(baz) df1 - data.frame(id, name) df2 - data.frame(id2, name2) v - df1$id[match(v,df1$name)] v [1] 1 1 2 2 NA A numeric vector. So here i actually want to return v [1] 1 1 2 2 baz Not possibly a numeric vector. so next time i can run v - df2$id[match(v,df2$name)] and return: v [1] 1 1 2 2 3 You need to keep track of the successful matches in df1 and then ypu probably want to interleave them with matches in df2. Perhaps instead use ifelse to do the work for you: ifelse(!is.na(match(v,df1$**name)), match(v,df1$name), match(v,df2$name2) ) [1] 1 1 2 2 1 Any help very much appreciated Mike [[alternative HTML version deleted]] __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. David Winsemius, MD West Hartford, CT [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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.