Remove element in a list that does not contain a pattern

Viewed 192

Hel lo maybe it is a silly question but I have a list such as :

> tiplabel
 [1] "SEQ_009163825"                                  "SEQ_009163870"                                 
 [3] "SEQ_009046380"                                  "SEQ_009046144"                                 
 [5] "SEQ_009448068"                                  "AXY04854"                                     
 [7] "SEQ_039113"                                     "AXY05113"                                     
 [9] "SEQ_612321"                                     "AMM72729"                                     
[11] "SEQ_+__CIO_sp"      "SEQ_-__CAZ_sp"  

and I would like to keep only element that contain __ two underscors.

and get

> tiplabel

[1]"SEQ_+__CIO_sp"     
[2]"SEQ_-__CAZ_sp"

Does someone have an idea ?

2 Answers

We can use grep with value = TRUE

grep('__', tiplabel, value = TRUE)
#[1] "SEQ_+__CIO_sp" "SEQ_-__CAZ_sp"

Or using stringr::str_subset

stringr::str_subset(tiplabel, '__')

You could use the %like% operator (loaded via the data.table package but from memory it's actually in another package):

library(data.table)
tiplabel[tiplabel %like% "__"]
Related