简体   繁体   English

R:用gsub替换“+”字符

[英]R: Replace “+” character with gsub

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. 由于某种原因,gsub()和sub()函数替换数值但保留运算符。 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 R 3.0.2

+ 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 "+" . 如果未转义, +表示“前一项将匹配一次或多次”,因此在第二个示例中, "5+""5"元素与模式匹配,并替换为"" ,留下"+"

使用fixed=TRUE选项:

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

You can also use strsplit : 你也可以使用strsplit

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM