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

Rolf Turner r.turner at auckland.ac.nz
Mon Nov 23 00:45:17 CET 2009


On 23/11/2009, at 12: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)
>
>
> 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.

(a) That's funny.  I don't get an error message when I try your example.
I get

[1] " a s d f + , j k l ? "

Of course that's not what you want, though.

(b) You need to escape the question mark:

  > gsub("\\?", " ", x)

yields

[1] "asdf+,jkl "

which I think *is* what you want.

	cheers,

		Rolf Turner



######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}




More information about the R-help mailing list