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
  • 178,638
  • 15
  • 253
  • 268
R. Joe
  • 367
  • 4
  • 12

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
  • 53,125
  • 10
  • 108
  • 134
2

We can use str_detect

library(stringr)
x[str_detect(x, "E14|K38")]
#[1] "*D46*E146*N189" "*B491*K380"     "*K389*E142X"   
akrun
  • 789,025
  • 32
  • 460
  • 575