Description Usage Arguments Details Value Warning Examples
Find (via potentially fuzzy matching) values in a table given a lookup reference
1 2 |
ref |
a vector of reference values to lookup |
tbl.ref |
reference values in the table to be matched to |
tbl.val |
values to be retrieved from the table when found in association with |
exact |
logical, default FALSE; if TRUE, matches of |
When exact=FALSE
, match.tbl
performs a match
, with the added utility of returning a value in the table (rather than simply the index of the matches). When exact=TRUE
, functions in cull
are called to reformat ref
and search for a match. If not match is found still, then fuzzy matching is performed via agrep
on versions of ref that have and have not been formatted via cull
.
The values of val.src
indicate the amount of fuzziness involved in the match:
m1 | an exact match |
m2 | exact match after cull |
m3 | fuzzy match performed on ref |
m4 | fuzzy match performed on cull(ref)
|
Fuzzy matching performed with agrep
, with arguments ignore.case=TRUE, max.distance=0.25
.
A data.table with 4 columns:
[,1] | ref | class(ref) | the reference value |
[,2] | val | class(val) | the value from the table (usually this is the desired output) |
[,3] | val.src | character | the source of the match |
[,4] | tbl.row | integer | Row of match in the table |
I am suspicous that the values returned in tbl.ref
may be inaccurate. However, this quality, and the function in general, has not been thoroughly tested. Although use-cases have given desirable results, albeit I think that the fuzzy matching can be a bit too fuzzy (finding matches where there shouldn't be any). Be aware.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | library(data.table)
tbl <- data.table(animal=c(
"cats",
"dogs",
"elephant",
"giraffe",
"monkey",
"person",
"Gadus morhua",
"Paralichthys dentatus",
"Pomatomus saltatrix",
"Amphiprioninae"
), a=1:10, b=10:1)
ref <- c(
"GADUS MORHUA",
"Amphiprion (the computer)",
"elehpant",
"dogs",
"squirrel",
"gaus"
)
tbl.ref <- tbl[,animal]
match.tbl(ref, tbl.ref, tbl[,animal]) # return what was matched to
match.tbl(ref, tbl.ref, tbl[,a]) # return another column
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.