[R] Backslash in sub pattern?

Stephan Kolassa Stephan.Kolassa at gmx.de
Mon Jul 14 20:09:27 CEST 2008

Dear guRus,

I am trying to replace "~" by "$\sim$" for TeX. However, I can't get the 
backslash to work. I would like to turn "DV~IV" into "DV$\sim$IV".

sub("~","$\sim$","DV~IV") => "DV$sim$IV"
sub("~","$\\sim$","DV~IV") => "DV$sim$IV"
sub("~","$\\\sim$","DV~IV") => "DV$sim$IV"
sub("~","$\\\\sim$","DV~IV") => "DV$\\sim$IV"

Alternatives 1 and 3 also yield warnings about unknown escape sequences 
(no surprise there). I have been unsuccessfully searching the archives. 
sessionInfo below.

Thank you all for your time!

Best regards

 > sessionInfo()
R version 2.7.1 (2008-06-23)


attached base packages:
[1] stats     graphics  grDevices utils     datasets  tcltk     methods 

other attached packages:
[1] svIDE_0.9-5

loaded via a namespace (and not attached):
[1] svMisc_0.9-5

More information about the R-help mailing list