问题
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