0

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

  • What is the desired output for the input `tBodyAcc-mean()-X` ? – Tim Biegeleisen Nov 25 '16 at 14:53
  • Welcome to Stackoverflow, I would suggest you to read first [how to ask questions on stackoverlfow](http://stackoverflow.com/help/how-to-ask) before posting. I would also point you toward the following topic on [how to replace regular expressions in R](http://stackoverflow.com/questions/11936339/in-r-how-do-i-replace-text-within-a-string). – Pierre Chevallier Nov 25 '16 at 14:56
  • Perhaps, you can just use a literal replacement: `mutate(feature,feature=str_replace(feature$feature,fixed("()"),""))` – Wiktor Stribiżew Nov 25 '16 at 14:56
  • for `sub`, you might use `sub("()", "", stringName, fixed=TRUE)`. This tells R to treat "()" as literal characters rather than special regular expression characters. – lmo Nov 25 '16 at 14:56
  • This will help: http://stackoverflow.com/questions/11936339/in-r-how-do-i-replace-text-within-a-string – tomp Nov 25 '16 at 14:58
  • Thank you Wiktor, works great. – Larry Tomaziefski Nov 25 '16 at 17:36

2 Answers2

0

Sub, gsub

\\ identify special characters

If you want to replace ONLY the parenthesis that are in the middle of the string (that is not at the start or at the end):

text <- "tBodyAcc-mean()-X"
sub("#\\(\\)#", "", text)
[1] "tBodyAcc-mean-X"

text <- "tBodyAcc-mean-X()"
sub("#\\(\\)#", "", text)
[1] "tBodyAcc-mean-X()"

If you want to replace ANY parenthesis (including those at the end and at the start of the string)

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

EDIT, as pointed out in several comments using gsub instead of sub will replace all the "()" in a string, while sub only replace the first "()"

text <- "t()BodyAcc-mean()-X"

sub("\\(\\)", "", text)
[1] "tBodyAcc-mean()-X"

> gsub("\\(\\)", "", text)
[1] "tBodyAcc-mean-X"
Gerald T
  • 704
  • 3
  • 18
  • Thank Gerald. Adding this explanation to my toolbox as well. Appreciate the help! – Larry Tomaziefski Nov 25 '16 at 17:38
  • @LarryTomaziefski Using sub you will only replace one time (). If you have a string with more () to replace, you should use gsub, as pointed in my answer. – Carles Mitjans Nov 25 '16 at 17:45
  • @CarlesMitjans for the problem I am working on sub would work, but in future practice I will keep sub in mind. Thanks again! – Larry Tomaziefski Nov 25 '16 at 18:09
  • @Larry Carles is right, gsub replace multiple times, sub only one. Just to clarify my codes above...the first one only remove parenthesis that are in the middle of the string, the second remove any. – Gerald T Nov 26 '16 at 12:29
0

You can do better using gsub. It will replace all occurrences.

# First argument is the pattern to find. Instead of () you specify \\(\\) because is a regular expression and you want the literal ()
# Second argument is the string to replace
# Third argument is the string in which the replacement takes place
gsub("\\(\\)", "REPLACE", "tBodyAcc-mean()-X")

Output:

[1] "tBodyAcc-meanREPLACE-X"
Carles Mitjans
  • 4,786
  • 3
  • 19
  • 38