Value matching or partial matching
pmatch2
returns a list of the positions
of matches or partial matches of x
in
table
.
This does sloppy matching to find "Peter" to match
"Pete" only if "Pete" is not in table
, and
we want "John Peter" if neither "Pete" nor "Peter"
are in table
.
pmatch2(x, table)
x |
the values to be matched |
table |
the values to be matched against |
1. nx <- length(x);
out <- vector(nx, "list");
names(out) <- x
2. for(ix in seq(length=nx))
:
3. xi <- which(x[ix] %in% table)
4. if(length(xi)<1)
xi <- grep(paste0('^', x[ix]), table)
.
5. if(length(xi)<1)xi <-
grep(x[ix], table)
.
6. out[[ix]] <- xi
A list of integer vectors indicating the
positions in table
matching each element
of x
Spencer Graves
## ## 1. common examples ## x2match <- c('Pete', 'Peter', 'Ma', 'Mo', 'Paul', 'Cardenas') tbl <- c('Peter', 'Mary', 'Martha', 'John Paul', 'Peter', 'Cardenas', 'Cardenas') x2mtchd <- pmatch2(x2match, tbl) # answer x2mtchd. <- list(Pete=c(1, 5), Peter=c(1, 5), Ma=2:3, Mo=integer(0), Paul=4, Cardenas=6:7) all.equal(x2mtchd, x2mtchd.) ## ## 2. strange cases that caused errors and are now warnings ## huh <- pmatch2("(7", tbl) # answer huh. <- list("(7"=integer(0)) all.equal(huh, huh.)
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.