XR SC XR SC - 10 days ago 5
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:

res=c("THIS","THIS","THIS","","")


I tried
gsub
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

We can use str_extract

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

It is better to have NA rather than ""