Skip to content

How to use escape characters in a string

3 messages · mary guo, Bert Gunter, David Winsemius

#
Please read R FAQ 7.37.

You need to escape the backslash:
[1] "`X^`R\\`S"
`X^`R\`S

Bert Gunter
Genentech Nonclinical Statistics


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of mary guo
Sent: Friday, January 29, 2010 1:58 PM
To: r-help at r-project.org
Subject: [R] How to use escape characters in a string

Hi,
I want to use a character as below in R,

 as.character("`X^`R\`S")
[1] "`X^`R`S"
Warning messages:
1: '\`' is an unrecognized escape in a character string
2: unrecognized escape removed from "`X^`R\`S"

But I found errors.
Can I use some option in as.character() to change this?

Thanks

Mary


______________________________________________
R-help at 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.
#
On Jan 29, 2010, at 4:58 PM, mary guo wrote:

            
What character?
> nchar("`X^`R\`S")
[1] 7
Warning messages:
1: '\`' is an unrecognized escape in a character string
2: unrecognized escape removed from "`X^`R\`S"
 > substr("`X^`R\`S" , 7,7)
[1] "S"
Warning messages:
1: '\`' is an unrecognized escape in a character string
2: unrecognized escape removed from "`X^`R\`S"
 > substr("`X^`R\`S" , 6,6)
[1] "`"
Warning messages:
1: '\`' is an unrecognized escape in a character string
2: unrecognized escape removed from "`X^`R\`S"
 > substr("`X^`R\`S" , 5,5)
[1] "R"
What do you want? Just to add a back-slash?

 > nchar("`X^`R\\`S")
[1] 8
 > substr("`X^`R\\`S" , 6,6)
[1] "\\"
Notice that one of the backslashes is not counted because it is not  
present in the internal representation. It is, however, printed with  
doubling.