How to remove + (plus sign) from string in R?

别来无恙 提交于 2019-12-10 17:31:37

问题


Say I use gsub and want to remove the following (=,+,-) sign from the string and replace with an underscore.

Can someone describe what is going on when I try to use the gsub with a plus sign (+).

test<- "sandwich=bread-mustard+ketchup"
# [1] "sandwich=bread-mustard+ketchup"

test<-gsub("-","_",test)
# [1] "sandwich=bread_mustard+ketchup"

test<-gsub("=","_",test)
# [1] "sandwich_bread_mustard+ketchup"

test<-gsub("+","_",test)
#[1] "_s_a_n_d_w_i_c_h___b_r_e_a_d___m_u_s_t_a_r_d_+_k_e_t_c_h_u_p_"

回答1:


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.




回答2:


test<-gsub("+","_",test,fixed = TRUE)

credit to Jota




回答3:


I was also stuck. the following code worked for me.

test<- "sandwich=bread-mustard+ketchup"
test<-gsub("\\+","_",test)
test
[1] "sandwich=bread-mustard_ketchup"

However, one time it didn't work. I tried with Ian's solution . It worked.



来源:https://stackoverflow.com/questions/35807677/how-to-remove-plus-sign-from-string-in-r

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