Larry Tomaziefski Larry Tomaziefski - 10 days ago 8
R Question

Trying to replace a () in a string in R using str_replace

I am trying to replace a

()
in a string using the
sub_string
function in R but it appears that due that the function is overlooking the
()
. I am pretty new to coding and R so I imagine that it has something to do with the regular expression of
()
.

I just dont know how to make the code identify that I want it to treat the
()
as regular characters

example string:

tBodyAcc-mean()-X


Here is the function I am using:

mutate(feature,feature=str_replace(feature$feature,(),""))


Appreciate the help

Answer

\ identify special character

if the parenthesis are in the middle of the string:

text <- "tBodyAcc-mean()-X"
sub("*\\(\\)*", "", text)

if the parenthesis are located anywhere in the string

text <- "tBodyAcc-mean()-X"
sub("\\(\\)", "", text)
Comments