我继承了一个 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"
但是,唉,运气不好。
我意识到答案非常简单,但我一直无法弄清楚如何做到这一点。
提前致谢。
最佳答案
您需要使用正则表达式以及 gsub 函数。我认为这个问题在这里得到了回答:In R, replace text within a string
希望这有帮助。
关于string - R 替换字符串中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18070251/