[R] How do I get sub to insert a single backslash?

From: Michael Dewey <info_at_aghmed.fsnet.co.uk>
Date: Fri 06 Jan 2006 - 08:08:22 EST


Something about the way R processes backslashes is defeating me. Perhaps this is because I have only just started using R for text processing.

I would like to change occurrences of the ampersand & into ampersand preceded by a backslash.

> temp <- "R & D"
> sub("&", "\&", temp)

[1] "R & D"
> sub("&", "\\&", temp)

[1] "R & D"
> sub("&", "\\\&", temp)

[1] "R & D"
> sub("&", "\\\\&", temp)

[1] "R \\& D"
>

So I can get zero, or two backslashes, but not one. I am sure this is really simple but I did not find the answer by doing, for example ?regexp or ?Quotes

Michael Dewey
http://www.aghmed.fsnet.co.uk



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 Fri Jan 06 08:25:25 2006

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:41:53 EST