Error: '\\R' is an unrecognized escape in character string starting “C:\\R”

匿名 (未验证) 提交于 2019-12-03 02:20:02

问题:

I am running Windows XP Pro and R Version 2.15.1 R is installed in the following folder:

C:\Program Files\R\R-2.15.1

I am trying to create a function that reads in a .csv file like so:

xxx 

I get the error

Error: '\R' is an unrecognized escape in character string starting "C:\R"

Is there a problem with my directory structure / folder naming conventions?

回答1:

You have to escape the \ since it is itself an escape character.

read.table('C:\\xxx\\classes\\R_Prog\\specdata\\data.csv') head(data) }



回答2:

As nobody suggested a forward slash yet, allow me to do so:

R> list.files("C:/opt", pattern="R") [1] "R-current"  "R-library"  "R-local215" "RStudio"    "Rtools"     R>  

I find forward slashes "easier on the eye" as it makes paths more consistent across OSs, and you do not need to escape them either. Which means you save a whole byte each time. Yippie.



回答3:

Noone has suggested file.path yet. This will concatenate a string together to form a file path using a platform specific separator ( default is / on windows)

file.path('c:', 'xxx', 'classes', 'R_prog','specdata', 'data.csv') ## [1] "c:/xxx/classes/R_prog/specdata/data.csv" 


回答4:

You need to escape your backslashes. try doubling them: c:\\xxx\\classes\\R_Prog\\ etc.



回答5:

I've found that both the \ (escaping the )

C:\\xxx\\classes\\R_Prog\\specdata\\data.csv 

and the / solutions work:

C:/xxx/classes/R_prog/specdata/data.csv 

I personally find it easier to use the latter.



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