[R] How to use escape characters in a string

1 view
Skip to first unread message

mary guo

unread,
Jan 29, 2010, 4:58:29 PM1/29/10
to r-h...@r-project.org
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

[[alternative HTML version deleted]]

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

Bert Gunter

unread,
Jan 29, 2010, 5:52:50 PM1/29/10
to mary guo, r-h...@r-project.org
Please read R FAQ 7.37.

You need to escape the backslash:
> z<- as.character("`X^`R\\`S")
> z
[1] "`X^`R\\`S"
> cat(z)
`X^`R\`S

Bert Gunter
Genentech Nonclinical Statistics

David Winsemius

unread,
Jan 29, 2010, 5:59:08 PM1/29/10
to mary guo, r-h...@r-project.org

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

Reply all
Reply to author
Forward
0 new messages