I have two vectors that I want to grep, but I want to keep the order in the pattern to grep. I solve it using a loop, although I'm wondering if there is any other (better) way of doing it.
EG.
to_match <- c("KZB8","KBB9","KBC9","KZA9","KZB2","KZB5","KZB6")
vectorA <- c("RuL_KZA9","RuL_KZB9","RuL_KZA5","RuL_KZC6","RuL_KZB8")
I solved like this:
matching <- c()
for (i in to_match){
t <- grep(i, vectorA, value = T)
matching <- c(matching,t)
}
> matching
[1] "RuL_KZB8" "RuL_KZA9"
BTW, I saw the great answers here: grep using a character vector with multiple patterns
But as you will see see the problem with:
grep(paste(to_match, collapse = "|"),vectorA, value = T)
[1] "RuL_KZA9" "RuL_KZB8"
is that the matching is sorted based on the first element that grep finds and not using the matching vector.
Thanks in advance for your ideas for a more efficient code.
Niko