Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove parenthesis from a character string

Tags:

string

r

gsub

I am trying to remove a parenthesis from a string in R and run into the following error:

string <- "log(M)" gsub("log", "", string) # Works just fine gsub("log(", "", string) #breaks # Error in gsub("log(", "", test) :  #   invalid regular expression 'log(', reason 'Missing ')'' 
like image 777
mike Avatar asked Feb 26 '12 00:02

mike


People also ask

How do I remove parentheses from a string?

To remove parentheses from string using Python, the easiest way is to use the Python sub() function from the re module. If your parentheses are on the beginning and end of your string, you can also use the strip() function.

How do I get rid of parentheses?

Parentheses can be removed by multiplying the outside factor to each term inside the parentheses. Note: A negative sign outside parentheses can be understood as the coefficient -1. The distributive property may then be applied to remove the parentheses.


1 Answers

Escape the parenthesis with a double-backslash:

gsub("log\\(", "", string) 

(Obligatory: http://xkcd.com/234/)

like image 157
Ben Bolker Avatar answered Sep 22 '22 14:09

Ben Bolker