简体   繁体   中英

how to use grep in R to select the correct id from a query

I am trying to use grep in R to select the correct id from a huge list of id's. It works for most of them but not for others. For example when i use this

> grep("cp_gi_88656873.2", names(data), value = T)
[1] "cp_gi_88656873.2"  "cp_gi_88656873.29"

it is giving me two id's rather than one. To make this more stringent i am adding a $ to the end of the query and it works ok

> grep("cp_gi_88656873.2$", names(data), value = T)
[1] "cp_gi_88656873.2"

Is there other ways around to solve this problem without adding $ to the end of id's? I ask this i have several hundred id's and i have to add $ to each one of them.

You can add the \\\\b at the beginning and end of the pattern.

grep('\\bcp_gi_88656873.2\\b', v1, value=TRUE)
#[1] "cp_gi_88656873.2"

data

 v1 <- c("cp_gi_88656873.2", "cp_gi_88656873.29")

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