XR SC XR SC - 1 year ago 109
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".

Answer Source

We can use str_extract

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

It is better to have NA rather than ""

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download