Reputation: 6885
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.
Upvotes: 2
Views: 28874
Reputation: 36
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)
Upvotes: 1
Reputation: 4421
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"
Upvotes: 0
Reputation: 21
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.
Upvotes: 2