inserting one backslash

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

inserting one backslash

RH Koning
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)?

I am using R 2.1.0 on suse linux 9.2.

Thanks, Ruud

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: inserting one backslash

Sundar Dorai-Raj

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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: inserting one backslash

Brian Ripley
In reply to this post by RH Koning
On Wed, 1 Feb 2006, 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

You are confusing the object and the printed representation: see ?regexp.

> cat(gsub("%","\\\\%","income 0%-33%"), "\n")
income 0\%-33\%

You really do want the object which prints in R "income 0\\%-33\\%".

> 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)?
>
> I am using R 2.1.0 on suse linux 9.2.

Time for an undate (not that it affects this, but how objects print has
been changed).

--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: inserting one backslash

Seth Falcon-2
In reply to this post by RH Koning
On  1 Feb 2006, [hidden email] 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)?

I think you are on the right track with the second pattern "\\\\%".
There is a difference between what print(s) will display and what
cat(s) will display.  


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

> cat(gsub("%", "\\\\%", "income 0%-33%"), "\n")
income 0\%-33\%  

The string "\\" in R contains one character (see nchar()), not two.  
It can be confusing.

HTH,

+ seth

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html