[R] How to use escape characters in a string

David Winsemius dwinsemius at comcast.net
Fri Jan 29 23:59:08 CET 2010


On Jan 29, 2010, at 4:58 PM, mary guo wrote:

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

What character?
>
> 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"

 > 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"

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

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.

-- 
David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list