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.

Leroy Tyrone
  • 427
  • 1
  • 5
  • 16
  • 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

2 Answers2

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
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
  • 3,421
  • 7
  • 48
  • 73