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

HTH

Peter

______________________________________________
R-help@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.

Reply via email to