Remove Parentheses in Character String in R (Example)
In this R tutorial you’ll learn how to delete parentheses in a character string.
Table of contents:
So now the part you have been waiting for – the example!
Creating Example Data
Let’s first create some example data:
my_string <- "aaa (bb) c ()" # Create example character string my_string # Print example character string # [1] "aaa (bb) c ()"
As you can see based on the previous output of the RStudio console, our example data is a character string containing different letters as well as some parentheses.
Example: Remove Parentheses in Character String Using gsub() Function
In this example, I’ll illustrate how to get rid of all round brackets in a character string.
For this task, we can apply the gsub function as shown below. Note that we are wrapping square brackets around the parentheses, since parentheses are considered as special characters by the gsub function:
my_string_new <- gsub("[()]", "", my_string) # Apply gsub function my_string_new # Print updated character string # [1] "aaa bb c "
Have a look at the previous output: We have removed all parentheses from our character string!
By the way, it is also possible to remove parentheses and the text within using the following syntax for the gsub function:
my_string_new2 <- gsub("\\s*\\([^\\)]+\\)", "", my_string) # Also remove text within () my_string_new2 # Print updated character string # [1] "aaa c ()"
Note that this code keeps parentheses without text in between.
Video & Further Resources
In case you need further explanations on the R programming codes of the present article, you may want to watch the following video on my YouTube channel. I’m explaining the R programming code of this tutorial in the video.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you may want to read some of the other articles on my website. You can find a selection of articles below.
- Remove Newline from Character String
- Remove All White Space from Character String
- R Programming Overview
On this page you have learned how to extract parentheses in a string in the R programming language. If you have additional questions or comments, don’t hesitate to let me know in the comments below.
Statistics Globe Newsletter
4 Comments. Leave new
how do we remove values inside the square brackets in R for an entire dataframe for each column “hello[1,2,3]”
Hey Charles,
It seems like you already got help here, or do you still need help with this question?
Regards,
Joachim
What if I want to remove the text within the bracket and the bracket in the following,
hello [00:01:20] my name is Jack.
so it prints out as Hello my name is Jack.
Hello Janet,
You can use the following:
text <- "hello [00:01:20] my name is Jack." pattern <- "\\[.*?\\]" # Remove the text within the brackets and the brackets themselves cleaned_text <- gsub(pattern, "", text) # Remove any double whitespaces cleaned_text <- gsub("\\s{2,}", " ", cleaned_text) cleaned_text # Output: "hello my name is Jack." Regards, Cansu