0

I am trying to replace the characters ST with Street, without impacting other values within the column, such as the word Stretch. I have found that the methods attempted so far do not change to ST when it is at the end of the string, such as: 100 60TH AVE & 96TH ST or I get something like "203 StreetREET" where Street has replaced ST on words that are currently STREET. I have tried to use

str_replace(oil_one$Facility, c("ST"), "Street")

&

oil_one$Facility[oil_one$Facility== "ST"] <- "Street"

Could somebody please demonstrate how to correctly do this on the column of a data frame?

Peter
  • 11,500
  • 5
  • 21
  • 31
OJJ
  • 29
  • 6
  • Welcome to Stackoverflow. To get a solution that will work for your data can only be done if you either set out all the cases in your data or even better include a sample of `oil_one$Facility` use `dput(oil_one$Facility)` to paste this information in the questions. Guidance have a look at [MRE]. Try searching for `dplyr::mulate()` as a possible solution. – Peter Apr 08 '21 at 19:37
  • Thanks for the welcome Peter - I tried Gregor's method and it worked. – OJJ Apr 09 '21 at 13:03
  • Will check out the mre now! – OJJ Apr 09 '21 at 13:03

1 Answers1

1

You can use \b for word boundaries to only replace ST when it stands alone.

str_replace(oil_one$Facility, "\\bST\\b", "Street")

You might also want to make it ignore case:

str_replace(oil_one$Facility, regex("\\bST\\b", ignore_case = TRUE), "Street")
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294