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

Jonathan

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_"
coffeinjunky

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Remove € sign from string

From Dev

Remove plus sign (+) in URL query string

From Dev

Remove plus sign from url using .htaccess

From Dev

remove dollar sign from string

From Dev

How to remove leading Plus sign inside parentheses

From Dev

Java-How to remove sign '$' from string '$49monthly'

From Dev

Remove dot . sign from the end of the string

From Dev

Plus sign in query string?

From Dev

Plus sign in query string?

From Dev

How to remove a specific pattern from a string in R?

From Dev

How to remove \n and \r from a string

From Dev

How to remove single quote from a string in R?

From Dev

How to remove a specific pattern from a string in R?

From Dev

How to remove '\' from a string using R?

From Dev

How to remove the plus sign (+) displayed on GMap centre location?

From Dev

How to remove eol (plus sign) character in postgres batch output

From Dev

How to remove the plus sign (+) displayed on GMap centre location?

From Dev

How to remove eol (plus sign) character in postgres batch output

From Dev

Remove last character from string in sql plus

From Dev

How to remove a £ sign from a MySQL database

From Dev

How to remove a custom sign from a variable

From Dev

How to encrypt query string avoiding plus sign and slashes and backslashes?

From Dev

Split a string by a plus sign (+) character

From Dev

Replace Plus sign in string php

From Dev

Split a string by a plus sign (+) character

From Dev

How can I prevent LibreOffice Calc from removing the plus (+) sign?

From Dev

How to remove � from a String?

From Dev

How to remove " [ ] \ from string

From Dev

How to remove ' from a string

Related Related

HotTag

Archive