Replace “\” with “/” in r [duplicate]

 ̄綄美尐妖づ 提交于 2019-12-24 16:45:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!