R: Replace “+” character with gsub

别等时光非礼了梦想. 提交于 2019-12-13 13:26:50

问题


the question seems totally trivial but I cannot figure out why it isn't working. I simply want to replace a character variable involving a "+" operator with a single value excluding the "+" operator. For some reason gsub() and sub() function replace the number value but keep the operator. Any hint on how this can be overcome? Many thanks!

data <- c(1,2,3,4,"5+")
gsub(pattern="5+",replacement="5",x=data)
#[1] "1"  "2"  "3"  "4"  "5+"

gsub(pattern="5+",replacement="",x=data)
#[1] "1"  "2"  "3"  "4"  "+"

R 3.0.2


回答1:


+ is a metacharacter, and needs to be escaped when you want to match it:

gsub(pattern="5\\+",replacement="5",x=data)
#[1] "1" "2" "3" "4" "5"

Or more generally, if you want to remove the +:

gsub(pattern="\\+",replacement="",x=data)

If unescaped, + means "The preceding item will be matched one or more times", so in your second example, the "5" element of "5+" is matched by the pattern, and replaced by "", leaving you with "+".




回答2:


Use fixed=TRUE option:

gsub(pattern="+", replacement="", fixed=TRUE, c(1,2,3,4,"5+"))




回答3:


You can also use strsplit:

as.numeric(strsplit(data, "\\+"))
# [1] 1 2 3 4 5


来源:https://stackoverflow.com/questions/20046526/r-replace-character-with-gsub

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