2

I have inherited an r project that has a dataframe with truncated text strings. Based on some responses in this forum, I have tried

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

This did not work. I 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. I also tried

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

but, alas, no luck.

I realise the answer is ridiculously simple but I haven't been able to work out how to do it.

Thanks in advance.

L Tyrone
  • 1,268
  • 3
  • 15
  • 24
  • Does this answer your question? [Replace specific characters within strings](https://stackoverflow.com/questions/11936339/replace-specific-characters-within-strings) – zx8754 Mar 29 '22 at 09:53

3 Answers3

2

You need to make use of regular expressions as well as the gsub function. I think this question is answered here: In R, replace text within a string

Hope this helps.

Community
  • 1
  • 1
1
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)
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
ronir
  • 36
  • 1
0

Use the stringi package:

require(stringi)

mydf<-data.frame(c("\bDistr\b", "\bDistr\b", "\bDistr\b", "\bDistr\b"))
stri_replace_all(mydf[,1], "", fixed="\b")
[1] "Distr" "Distr" "Distr" "Distr"
MERose
  • 4,048
  • 7
  • 53
  • 79