rstringgrepgsub

Replace truncated word in a string


I have inherited an R project that has a dataframe with truncated text strings.

temp <- data.frame(taname2 = c("ends with Distr", "distr at start", "the word distr in the middle", "distr", "district"))

Based on some answers on SO, I have tried:

temp$taname2 <- gsub("\bDistr\b", "District", temp$taname2)

This did not work. I also tried:

temp$taname2 <- gsub("Distr", "District", temp$taname2)

but this resulted in rows already containing the word "District" being changed to "Districtic", "Districtict", or similar. This:

temp$taname2[grep("\bDistr\b",temp$taname2)] <- "District"

also did not work.


Solution

  • gsub("\\s+Distr$|^Distr\\s+|\\s+Distr\\s+|^Distr$", " District ", 
         c("ends with Distr", "distr at start", "the word distr in the middle", "distr", "district"),  ignore.case = T)