Grendel
Grendel

Reputation: 783

Remove element in a list that does not contain a pattern

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 ?

Upvotes: 2

Views: 567

Answers (2)

rg255
rg255

Reputation: 4169

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% "__"]

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388862

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, '__')

Upvotes: 1

Related Questions