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?

0

2 Answers 2

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)] 
Sign up to request clarification or add additional context in comments.

Comments

2

We can use str_detect

library(stringr) x[str_detect(x, "E14|K38")] #[1] "*D46*E146*N189" "*B491*K380" "*K389*E142X" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.