Try
test<- "sandwich=bread-mustard+ketchup"
test<-gsub("\+","_",test)
test
[1] "sandwich=bread-mustard_ketchup"
+
is a special character. You need to escape it. Same as, for instance, .
. If you google regex
or regular expressions, you will find the corresponding lists of special characters. For instance, here +
is described to indicate 1 or more of previous expression
. More about special characters, regular expressions and R can be found here or here.
On a more general note, your above code could be written more efficiently by using:
test<- "sandwich=bread-mustard+ketchup"
test<-gsub("[-|=|\+]","_",test)
test
[1] "sandwich_bread_mustard_ketchup"
Here I have used a construct that can basically be read as [either this or that or something else]
, where |
corresponds to or
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…