1

In the following vector:

x<-c("*D46*E146*N189", "*M420", "*B491*K380", "*Y841*N189*N179", "*K389*E142X")

I would like to extract the elements that contain "E14" and "K38".

That is, I would like to have returned the elements: "*D46*E146*N189", "*B491*K380", and "*K389*E142X".

Does anyone have any suggestion?

MrFlick
  • 195,160
  • 17
  • 277
  • 295
R. Joe
  • 367
  • 5
  • 13

2 Answers2

3

You can use grep

> grep("E14|K38", x, value = TRUE)
[1] "*D46*E146*N189" "*B491*K380"     "*K389*E142X"

Or indexing using R base grep or grepl

x[grep("E14|K38", x)]
x[grepl("E14|K38", x)]
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
2

We can use str_detect

library(stringr)
x[str_detect(x, "E14|K38")]
#[1] "*D46*E146*N189" "*B491*K380"     "*K389*E142X"   
akrun
  • 874,273
  • 37
  • 540
  • 662