Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R – gsub – replace character exact match

I am using the following code to replace string characters with empty strings:

description_clean_df$description_clean <- gsub("amp", "", description_clean_df$description_clean)

How can I have this as exact match? E.g. I want to replace exactly "amp", but not other words which contain "amp", such as the word "example"? How can I do that?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Add the wordboundary (\\b) before and after the word ‘amp’ so that it won’t match any words with a substring ‘amp’ in it

description_clean_df$description_clean <- gsub("\\bamp\\b", "", 
    description_clean_df$description_clean)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading