Reputation: 839
I was wondering how it is possible to do partial matching in R?
I wanted to do partial matching to order file names in a list based on another vector like order. order and filenames have a feature in common!
files:
gh456_rr
FFF432
tw4522
order <- c("432","522","456")
files <- list.files()
files <- files[ pmatch(order, files) ]
but charmatch pmatch are not available even for R version below 2.10!!
Upvotes: 0
Views: 609
Reputation: 7654
The problem appears to me to be the mixture characters and digits. I am running the current version of R and tried these variations:
> charmatch(c("gh456_rr", "FFF432", "tw4522"), order <- c("432","522","456"))
[1] NA NA NA
> charmatch(c("", "a432", "a123"), c("1", "123", "432")) # no matches because of letter preceding number
[1] 0 NA NA
> charmatch(c("", "432a", "a123"), c("1", "123", "432")) # same because of letter following
[1] 0 NA NA
> charmatch(c("", "432", "a123"), c("1", "123", "432")) # matches second element "432" to third element "432"
[1] 0 3 NA
> pmatch(c("456_rr", "432", "522"), order <- c("432","522","456")) # doesn't match the first element
[1] NA 1 2
> pmatch(c("456_rr", "432a", "522"), order <- c("432","522","456")) # mixtures of digits and chars doesn't match
[1] NA NA 2
> pmatch(c("gh456_rr", "432a", "t522"), order <- c("432","522","456")) # mixtures of digits and chars doesn't match
[1] NA NA NA
Have you tried agrep()?
Upvotes: 1