R: agrep with vector drawing
I have a vector of templates and need to use agrep
. The problem is agrep
apparently only accepts one template at a time.
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))
}
Result:
[1] "rest" "tesr"
[1] "1 Barrel" "10 Barrel Brewing" "Harpoon 100 Barrel Series"
for
slower on long templates, trying to do it in vectorized form:
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
Could there be features like lapply
etc? Thank!!
+3
source to share