问题
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?
回答1:
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"
来源:https://stackoverflow.com/questions/48481245/replace-with-in-r