[R] gsub: replacing double backslashes with single backslash

Markus Elze m.elze at warwick.ac.uk
Wed Mar 7 12:54:24 CET 2012

Hello everybody,
this might be a trivial question, but I have been unable to find this using 
Google. I am trying to replace double backslashes with single backslashes using 
gsub. There seems to be some unexpected behaviour with regards to the 
replacement string "\\". The following example uses the string C:\\ which should 
be converted to C:\ .

 > gsub("\\\\", "\\", "C:\\")
[1] "C:"
 > gsub("\\\\", "Test", "C:\\")
[1] "C:Test"
 > gsub("\\\\", "\\\\", "C:\\")
[1] "C:\\"

I have observed similar behaviour for fixed=TRUE and perl=TRUE. I use R 2.14.1 
64-bit on Windows 7.


Markus Elze

Department of Statistics
University of Warwick

More information about the R-help mailing list