Removing parentheses in R

I am trying to remove the brackets from a string value in this case:

(40.703707008, -73.943257966)

I can not find a message with code that works; I know this is a very simple task, but I have seen the following links, but they either kill all my punctuation or do not work. Below are the codes I tried. Appreciate the help:

remove brackets from string

Remove parentheses and text inside lines from R

x = ("(40.703707008, -73.943257966)")
gsub("\\s*\\([^\\)]+\\)","",x)
gsub("\\D", "", x)
gsub("log\\(", "", x)
+4
source share
1 answer

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

gsub("[()]", "", x)
#[1] "40.703707008, -73.943257966"
+7
source

Source: https://habr.com/ru/post/1660455/


All Articles