[R] How to convert "c:\a\b" to "c:/a/b"?

Spencer Graves spencer.graves at pdf.com
Mon Jun 27 18:41:19 CEST 2005


	  Thanks, Dirk, Gabor, Eric:

	  You all provided appropriate solutions for the stated problem. 
Sadly, I oversimplified the problem I was trying to solve:  I copy a 
character string giving a DOS path from MS Windows Explorer into an R 
script file, and I get something like the following:

	  D:\spencerg\statmtds\R\Rnews

	  I want to be able to use this in R with its non-R meaning, e.g., in 
readLine, count.fields, read.table, etc., after appending a file name. 
Your three solutions all work for my oversimplified toy example but are 
inadequate for the problem I really want to solve.

	  Thanks,
	  spencer graves

Gabor Grothendieck wrote:

> On 6/27/05, Dirk Eddelbuettel <edd at debian.org> wrote:
> 
>>On 26 June 2005 at 20:30, Spencer Graves wrote:
>>|         How can one convert back slashes to forward slashes, e.g, changing
>>| "c:\a\b" to "c:/a/b"?  I tried the following:
>>|
>>|  > gsub("\\\\", "/", "c:\a\b")
>>| [1] "c:\a\b"
>>
>>This does work, provided you remember that single backslashed "don't exist"
>>as e.g. \a is a character in itself. So use doubles are you should be fine:
>>
>>
>>>gsub("\\\\", "/", "c:\\a\\b")
>>
>>[1] "c:/a/b"
>>
> 
> 
> Also, if one finds four backslashes confusing one can avoid the use 
> of four via any of these:
> 
> gsub("[\\]", "/", "c:\\a\\b")
> gsub("\\", "/", "c:\\a\\b", fixed = TRUE)
> chartr("\\", "/", "c:\\a\\b")
> 
> ______________________________________________
> R-help at 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

-- 
Spencer Graves, PhD
Senior Development Engineer
PDF Solutions, Inc.
333 West San Carlos Street Suite 700
San Jose, CA 95110, USA

spencer.graves at pdf.com
www.pdf.com <http://www.pdf.com>
Tel:  408-938-4420
Fax: 408-280-7915




More information about the R-help mailing list