Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing parenthesis in R

Tags:

regex

r

gsub

I am trying to remove parentheses from a string value in this case this one:

(40.703707008, -73.943257966)

I can't seem to find a post with code that works; I know that this is a very simple task, but I've seen the following links but they either kill all my punctuation or don't seem to work. Below is the codes I've tried. Appreciate the help:

remove parenthesis from string

Remove parentheses and text within from strings in R

x = ("(40.703707008, -73.943257966)")
gsub("\\s*\\([^\\)]+\\)","",x)
gsub("\\D", "", x)
gsub("log\\(", "", x)
like image 416
LoF10 Avatar asked Nov 11 '16 00:11

LoF10


People also ask

How do I remove parentheses from a string?

Using the replace() Function to Remove Parentheses from String in Python. In Python, we use the replace() function to replace some portion of a string with another string. We can use this function to remove parentheses from string in Python by replacing their occurrences with an empty character.

What is parenthesis in R?

Round brackets (also known as "parenthesis") are used primarily when calling a function in R. Every function must be called using the round brackets. Some functions need additional information that must be provided to them inside the round brackets. This additional information is called the arguments of a function.


1 Answers

These are metacharacters that either needs to be escaped (with \\) or we can place it in a square bracket to read it as character.

gsub("[()]", "", x)
#[1] "40.703707008, -73.943257966"
like image 83
akrun Avatar answered Sep 27 '22 16:09

akrun