替换& quot; \& quot;与& quot;/& quot;在r

替换& quot; \& quot;与& quot;/& quot;在r

问题描述:

我正在尝试用R中的"/"或"\\"替换"\".

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"

很显然,"\"是转义字符,不能以这种方式使用.有没有办法避免在R中使用"\"作为转义字符?

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?

您可以使用扫描功能.在您的示例中:

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"

结果:

X
[1] "C:\\users\\jordan\\Documents\\Computer Science\\R\\miscData.txt"