2

I'm trying to replace "\" with "/" or "\\" in R.

fp = "C:\users\jordan\Documents\Computer Science\R\miscData.txt"
replace(fp, "\", "\\")


Output:
> fp = "C:\users\jordan\Documents\Computer Science\R\miscData.txt"
Error: '\u' used without hex digits in character string starting ""C:\u"

Obviously, "\" is an escape character and can't be used this way. Is there a way to avoid the use of "\" as an escape character in R?

Jordan
  • 73
  • 7

1 Answers1

0

You can use the scan function. In your example:

X = scan(what="character",allowEscapes=F, nmax = 1)
"C:\users\jordan\Documents\Computer Science\R\miscData.txt"

The result:

X
[1] "C:\\users\\jordan\\Documents\\Computer Science\\R\\miscData.txt"
Bruno Vilela
  • 103
  • 8