我正在尝试在R中编写代码,将字符串中的所有“非”单词更改为“none”,但我不想更改任何其他单词,特别是我不想将“none”更改为“nonee”。
我试过这段代码:
gsub("non","none", "non none", fixed = TRUE)
但结果是:
"none nonee"
无论如何使用R的gsub做到这一点?
答案 0 :(得分:0)
您可以使用字边界...
x <- c("non" , "none")
gsub( "\\bnon\\b" , "none" , x )
#[1] "none" "none"