How do I do an exact string match using gsub in R? [duplicate]

风流意气都作罢 提交于 2021-02-05 09:26:06

问题


raw = c("MOUNTAIN VIEW","MOUNTAIN")
x = gsub("MOUNTAIN", "MOUNTAIN VIEW", raw, ignore.case = TRUE)

Current output: "MOUNTAIN VIEW VIEW" "MOUNTAIN VIEW"  
Desired output:  "MOUNTAIN VIEW" "MOUNTAIN VIEW"  

I only want to replace the 2nd entry in the raw data MOUNTAIN with MOUNTAIN VIEW. The first entry in raw data is already correct. But when I do gsub it replaces both the occurrences of MOUNTAIN with MOUNTAIN VIEW. Can anyone help me find a way to get around that?

I tried \\b but it didn't work and I understand why. Is there any thing else I can do?


回答1:


Use anchors instead here to match the entire string:

sub('^MOUNTAIN$', 'MOUNTAIN VIEW', raw, ignore.case = TRUE)
# [1] "MOUNTAIN VIEW" "MOUNTAIN VIEW"

If you desire, you can also use a capturing group and backreference it inside the replacement call:

sub('^(MOUNTAIN)$', '\\1 VIEW', raw, ignore.case = TRUE)



回答2:


using agrep -returns vector indices, so you can easily assign a value by using subscripts:

raw[agrep("MOUNTAIN", raw)] <- "MOUNTAIN VIEW" 
raw
[1] "MOUNTAIN VIEW" "MOUNTAIN VIEW"


来源:https://stackoverflow.com/questions/30387685/how-do-i-do-an-exact-string-match-using-gsub-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!