Sean Zhang
2009-Dec-13 22:11 UTC
[R] how to replace a single backward slash with a double backward slash?
Dear R-helpers: Hours ago, I asked how to replace a single forward slash with a double backward slash and recieved great help. Thanks again for all the repliers. In the meantime, I wonder how to replace a single backward slash with a double backward slash? e.g., I want change "c:\test" into "c:\\test" I tried the following but does not work. gsub("\\\","\\\\","c:\test") Can someone help? Thanks a lot in advance. -Sean [[alternative HTML version deleted]]
William Dunlap
2009-Dec-13 22:24 UTC
[R] how to replace a single backward slash with a double backwardslash?
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Sean Zhang > Sent: Sunday, December 13, 2009 2:11 PM > To: r-help at r-project.org > Subject: [R] how to replace a single backward slash with a > double backwardslash? > > Dear R-helpers: > > Hours ago, I asked how to replace a single forward slash with a double > backward slash and recieved great help. Thanks again for all > the repliers. > > In the meantime, I wonder how to replace a single backward > slash with a > double backward slash? > > e.g., I want change "c:\test" into "c:\\test" > > I tried the following but does not work. > gsub("\\\","\\\\","c:\test")The input string "c:\test" contains no backslash character, nor a "t" character. It contains cee colon tab ("\t") ee ess tee If you output the string using cat() you will see the tab action and the missing "t". "\t" means tab, "\n" newline, "\r" carriage return, "\52" octal 52 (asterisk in ASCII encoding), "\x2a" hexadecimal 2a (also asterisk), etc. ?Syntax may give all the details. If you want to change tabs to the 2-character sequences backslash t, then do > gsub("\t", "\\\\t", "c:\test") [1] "c:\\test" > cat(.Last.value, "\n") c:\test Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> > Can someone help? > > Thanks a lot in advance. > > -Sean > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >
David Winsemius
2009-Dec-13 22:26 UTC
[R] how to replace a single backward slash with a double backward slash?
On Dec 13, 2009, at 5:11 PM, Sean Zhang wrote:> Dear R-helpers: > > Hours ago, I asked how to replace a single forward slash with a double > backward slash and recieved great help. Thanks again for all the > repliers. > > In the meantime, I wonder how to replace a single backward slash > with a > double backward slash? > > e.g., I want change "c:\test" into "c:\\test" > > I tried the following but does not work. > gsub("\\\","\\\\",) > > Can someone help?Your problem may be that you think there actually is a "\" in "c: \test". There isn't: > grep("\\\\", "c:\test") # which would have found a true "\" integer(0) It's an escaped "t", which is the tab character = "\t": > grep("\\\t", "c:\test") [1] 1 > cat("rr\tqq") rr qq If your goal is to make file paths in Windows correctly, then you have two choices: a) use doubled "\\"'s in the literal strings you type, or ... b) use "/"'s So maybe you should explain what you are doing? We don't request that background out of nosiness, but rather so we can give better answers -- David Winsemius, MD Heritage Laboratories West Hartford, CT
Reasonably Related Threads
- how to replace a single forward slash with a double backward slash in a string?
- mmap() Error, Invalid Argument - Double slash in path?
- linux/syslinux.c: Double slash question
- forward slash vs double backslash R and Tinn-R
- disable interpretation of trailing slash