5

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

Cyrus
  • 84,225
  • 14
  • 89
  • 153

1 Answers1

6

Try lapply:

unlist(lapply(to_match, grep, vectorA, value = TRUE))
## [1] "RuL_KZB8" "RuL_KZA9"

or

unlist(sapply(to_match, grep, vectorA, value = TRUE))
##       KZB8       KZA9 
## "RuL_KZB8" "RuL_KZA9" 
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341