11

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)
Community
  • 1
  • 1
LoF10
  • 1,907
  • 1
  • 23
  • 64

1 Answers1

24

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"
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 2
    Great solution. In case helpful to anyone else, in the case of square brackets you could do something along these lines: `gsub("\\[|\\]", "", x)` – Ricky Jun 20 '22 at 08:19