[R] Re placing a "+" in a string

Romain Francois romain.francois at dbmail.com
Thu May 21 17:50:01 CEST 2009


Tom La Bone wrote:
> I know this is easy, but I am stumped:
>  
>   
>> gsub("0","K","8.00+00")
>>     
> [1] "8.KK+KK"
>
>   
>> gsub("+","K","8.00+00")
>>     
> Error in gsub("+", "K", "8.00+00") : invalid regular expression '+'
> In addition: Warning message:
> In gsub("+", "K", "8.00+00") :
>   regcomp error:  'Invalid preceding regular expression'
>
> I don't understand the error message. How do I go about replacing the "+" in
> the string "8.00+00" with another character?
>   
+ has a special meaning in regular expressions

gsub( "[+]", "K", "8.00+00" )
gsub( "\\+", "K", "8.00+00" )
gsub( "+", "K", "8.00+00", fixed = TRUE )

see ?regex for details
> Tom
>   


-- 
Romain Francois
Independent R Consultant
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr



More information about the R-help mailing list