XR SC XR SC - 3 months ago 29
R Question

How to delete all strings except some specific letters in R?

after researching for a while, I didn't find exactly what I would like.

What I'd like to do is to keep an exact pattern in a string.

So this is my example:

text=c("hello, please keep THIS","THIS is important","all THIS should be done","not exactly This","not THHIS")

how to get exactly "THIS" in all strings:


I tried
in r, but I don't know how to match characters.

For example I tried:

gsub("(THIS).*", "\\1", text) # This delete all string after "THIS".

gsub(".*(THIS)", "\\1", text) # This delete all string before "THIS".


We can use str_extract

str_extract(text, "THIS")
#[1] "THIS" "THIS" "THIS" NA  

It is better to have NA rather than ""