[R] gsub issue in R 2.11.1, but not present in 2.9.2

Duncan Murdoch murdoch.duncan at gmail.com
Tue Jun 29 13:22:57 CEST 2010


On 29/06/2010 6:47 AM, Jason Rupert wrote:
> Previously in R 2.9.2 I used the following to convert from an improperly formatted NA string into one that is a bit more consistent.   
>
>
> gsub("N\A", "NA", "N\A", fixed=TRUE)
>
> This worked in R 2.9.2, but now in R 2.11.1 it doesn't seem to work an throws the following error. 
> Error: '\A' is an unrecognized escape in character string starting "N\A"
>
> I guess my questions are the following:
> (1) Is this expected behavior? 
>   

Yes.

> (2) If it is expected behavior, what is the proper way to replace "N\A" with "NA" and "N\\A" with "NA"?
>   

You need to and double the backslashes to enter them in an R string.  So

gsub("N\\A", "NA", original, fixed=TRUE)

should work if original contains a single backslash, and

gsub("N\\\\A", "NA", original, fixed=TRUE)

should work if it contains a double one.  Two things add to the confusion here:  First, a single backslash will be displayed doubled by print().  And secondly, if you don't use "fixed=TRUE", then you are working with regular expressions, and need another doubling, i.e. my first gsub() is equivalent to

gsub("N\\\\A", "NA", original)

and my second is equivalent to

gsub("N\\\\\\\\A", "NA", original)


Duncan Murdoch



More information about the R-help mailing list