2013-08-06 42 views
1

我已经继承了一个r项目,该项目具有截断文本字符串的数据帧。基于在这个论坛的一些反应,我已经尝试R替换字符串中的字

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

这没有奏效。我试过

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

但是这导致已经包含单词“District”的行被改为“Districtic”,“Districtict”或类似行。我也试过

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

但是,唉,没有运气。

我意识到答案是荒谬的简单,但我还没有能够解决如何做到这一点。

在此先感谢。

回答

0

使用stringi包:

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"