[R] Removing "+" and "?" signs

jim holtman jholtman at gmail.com
Mon Nov 23 00:38:59 CET 2009


'?' is a metacharacter in a regular expression.  You have to escape it:

> x <- "asdf+,jkl?"
>
> gsub("?", " ", x)
Error in gsub("?", " ", x) : invalid regular expression '?'
In addition: Warning message:
In gsub("?", " ", x) :
  regcomp error:  'Invalid preceding regular expression'
> # escape it
> gsub("\\?", " ", x)
[1] "asdf+,jkl "


On Sun, Nov 22, 2009 at 6:01 PM, Steven Kang <stochastickang at gmail.com> wrote:
> Hi all,
>
>
> I get an error message when trying to replace *+* or *?* signs (with empty
> space) from a string.
>
> x <- "asdf+,jkl?"
>
> gsub("?", " ", x)
>
>
> Error message:
>
> Error in
> gsub("?", " ", x) :
>  invalid regular expression '?'
> In addition: Warning message:
> In gsub("?", " ", x) :
>  regcomp error:  'Invalid preceding regular expression'
>
> Your expertise in resolving this issue would be appreciated.
>
> Thanks.
>
>
>
> Steven
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list