Re: [R] inserting one backslash

From: Sundar Dorai-Raj <sundar.dorai-raj_at_pdf.com>
Date: Thu 02 Feb 2006 - 05:05:26 EST

RH Koning wrote:
> Hello, I am not very familiar with regular expressions and escaping. I
> need to replace the %-signs in a character vector with elements as
> "income 0%-33%# to be replaced by "income 0\%-33\%" (for later use in
> LaTeX). Using
>
> gsub("%","\\%","income 0%-33%")
>
> does not give the desired result. However, gsub("%","\\\\%","income
> 0%-33%") gives "income 0\\%-33\\%", one backslash too much. What is the
> appropriate expression to get the desired output (one backslash before
> each %-sign)?

Actually, you got the answer. See the difference between:

 > gsub("%","\\\\%","income 0%-33%")
[1] "income 0\\%-33\\%"
 > cat(gsub("%","\\\\%","income 0%-33%")) income 0\%-33\%

HTH, --sundar



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Feb 02 05:42:07 2006

This archive was generated by hypermail 2.1.8 : Thu 02 Feb 2006 - 08:29:54 EST