Learn R Programming

Ecfun (version 0.1-3)

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 of "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

Value

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

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)

See Also

match pmatch grep matchName

Examples

Run this code
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)
stopifnot(
all.equal(x2mtchd, x2mtchd.)
)

Run the code above in your browser using DataLab