Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

pmatch2

Value matching or partial matching


Description

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.

Usage

pmatch2(x, table)

Arguments

x

the values to be matched

table

the values to be matched against

Details

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

Value

A list of integer vectors indicating the positions in table matching each element of x

Author(s)

Spencer Graves

See Also

Examples

##
## 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.)

Ecfun

Functions for 'Ecdat'

v0.2-4
GPL (>= 2)
Authors
Spencer Graves <spencer.graves@effectivedefense.org>
Initial release
2020-10-26

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.