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

David Winsemius dwinsemius at comcast.net
Mon Nov 23 01:06:46 CET 2009


On Nov 22, 2009, at 6:01 PM, Steven Kang 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)

Since both ? and + are special regex characters, to do both the  
substitutions at once you need to use double backslashes and an <or>

 > gsub("\\?|\\+", " ", x)
[1] "asdf ,jkl "

-- 
David

>
>
> 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list