adrCoder
adrCoder

Reputation: 3275

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?

Upvotes: 1

Views: 496

Answers (1)

akrun
akrun

Reputation: 886948

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)

Upvotes: 1

Related Questions