2

Possible Duplicate:
Ignore escape characters (backslashes) in R strings

I want to replace "\" in a string by "/" in a string. For example, the initial string is "d:\temp\1.txt" and I want to have "d:/temp/1.txt". I tried with gsub but it is not working as I want. However, if I do a simpler exercise

> gsub("a", "b", "banana")
[1] "bbnbnb"

It is working fine. Are there some tricks working with the special characters "/" and "\"?

Community
  • 1
  • 1
JACKY88
  • 3,391
  • 5
  • 32
  • 48
  • What happens if you call `cat(yourstring)`? That way you can see exactly how R is interpreting the input. Be careful though, because the string you have in your code will very likely be different than the result from pasting the string you've provided. – Justin Dec 14 '12 at 15:43

1 Answers1

2

@Paul, this wont work - see the R for Windows FAQ.

see what R does with the backslashes:

a <- "d:\temp\1.txt"
cat(a)
# d:      emp.txt

escaping like..

gsub("\\", "/", a)
Fehler in gsub("\\", "/", a) : 
  ungültiger regulärer Ausdruck '\', Grund 'Trailing backslash'

..does not work..

this would work:

b <- "d:\\temp\\1.txt"
cat(b)
# d:\temp\1.txt
gsub("\\\\", "/", b)
# "d:/temp/1.txt"
Kay
  • 2,702
  • 6
  • 32
  • 48