R中的部分匹配,charmatch pmatch不可用

时间:2015-02-24 02:53:52

标签: r

我想知道如何在R中进行部分匹配?

我想进行部分匹配,以便根据订单等其他矢量在列表中订购文件名。 订单和文件名有一个共同点!

files:
gh456_rr
FFF432
tw4522

order <- c("432","522","456")

    files <- list.files()

    files <- files[ pmatch(order, files) ]

但即使对于2.10以下的R版本,charmatch pmatch也不可用!!

1 个答案:

答案 0 :(得分:1)

在我看来,问题是混合字符和数字。我正在运行当前版本的R并尝试了这些变体:

> 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

您是否尝试过agrep()?