简体   繁体   中英

grep in R using a character vector with multiple patterns with same order as vector

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

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" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM