3

パターンのベクトルがあり、agrepそれらを使用する必要があります。問題は、agrep一度に 1 つのパターンしか取らないように見えることです。

patt <- c("test","10 Barrel")
lut  <- c("1 Barrel","10 Barrel Brewing","Harpoon 100 Barrel Series","resr","rest","tesr")

for (i in 1:length(patt)) {
  print(agrep(patt[i],lut,max=1,v=T))
}

結果:

[1] "rest" "tesr"
[1] "1 Barrel"                  "10 Barrel Brewing"         "Harpoon 100 Barrel Series"

for長いパターンでは遅いため、ベクトル化された形式で実行しようとします:

VecMatch1 = function(string, stringVector){
  stringVector[agrep(string, stringVector, max = 1)]
}
a = VecMatch1(patt,lut)

Warning message:
In agrep(string, stringVector, max = 1) :
  argument 'pattern' has length > 1 and only the first element will be used

lapplyなどの機能が役立つかもしれませんか?ありがとう!!

4

1 に答える 1

6

使用lapply:

lapply(patt, agrep, x=lut, max.distance=c(cost=1, all=1), value=TRUE)

[[1]]
[1] "rest" "tesr"

[[2]]
[1] "1 Barrel"                  "10 Barrel Brewing"         "Harpoon 100 Barrel Series"

dplyr または data.table を使用すると、パフォーマンスが向上する可能性があります。

于 2015-07-15T16:12:27.363 に答える