Home > Blockchain >  R - gsub - replace character exact match
R - gsub - replace character exact match

Time:09-11

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?

CodePudding user response:

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)
  • Related