[R] gsub() with unicode and escape character

Peter Langfelder peter.langfelder at gmail.com
Sun Jul 17 06:12:09 CEST 2011

Don't know the answer to you first question, but for the \\ see below.

On Sat, Jul 16, 2011 at 7:19 PM, Sverre Stausland
<johnsen at fas.harvard.edu> wrote:

> Unrelated to that problem, but related to gsub() is that I can't find
> a way for gsub() to interpret the backslash as a character. In regular
> expression, \\ should represent "the character \", but gsub() doesn't:
>> data.frame(animals=c("dog","wolf","cat"))->my.data
>> gsub("d","\\",my.data$animals)
> [1] "og"   "wolf" "cat"

Use \\\\ (yes, that's 4 backslashes).
> gsub("d","\\\\",my.data$animals)
[1] "\\og" "wolf" "cat"

> cat(paste(gsub("d","\\\\",my.data$animals)))
\og wolf cat>

The reason is that the backslashes get interpreted twice, once when
the command line parses the string, second time when the gsub
processes the pattern.



More information about the R-help mailing list