尝试读取CSV文件时“字符串中无法识别的转义”

时间:2012-05-02 16:33:48

标签: r import

我正在尝试导入.csv文件,以便我可以关注此视频:R ggplot2 Graphics Histograms

我安装了所有适当的软件包,包括ggplot和相关软件包。视频中的第一条指令表示要输入afl.df=read.csv("afl_2003_2007.csv")

所以,我下载了afl_2003_2007.csv文件,我尝试了以下所有内容,基本上将文件放在不同的目录中(共享驱动器,然后是C驱动器等)。我也尝试使用setwd,但没有运气。

我在Windows中使用R.

这是我尝试过的,以及我得到的错误:

> afl.df=read.csv("afl_2003_2007.csv")
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
  cannot open file 'afl_2003_2007.csv': No such file or directory
> afl.df=read.csv("\\the-lab.llnl.gov\llnlusers1\lopez235\Data\Documents\Dashboards,HRBI, Visulizations and Analytics\Math and Statistics and Predictive Modeling1\R Programming\afl_2003_2007.csv")
Error: '\l' is an unrecognized escape in character string starting "\\the-lab.llnl.gov\l"
> afl.df=read.csv("C:\Users\lopez235\Local-NOTBackedUp\R Files Local\afl_2003_2007.csv")
Error: '\U' used without hex digits in character string starting "C:\U"
> setwd("\\the-lab.llnl.gov\llnlusers1\lopez235\Data\Documents\Dashboards,HRBI, Visulizations and Analytics\Math and Statistics and Predictive Modeling1\R Programming\afl_2003_2007.csv")
Error: '\l' is an unrecognized escape in character string starting "\\the-lab.llnl.gov\l"
> setwd("\\the-lab.llnl.gov\llnlusers1\lopez235\Data\Documents\Dashboards,HRBI, Visulizations and Analytics\Math and Statistics and Predictive Modeling1\R Programming")
Error: '\l' is an unrecognized escape in character string starting "\\the-lab.llnl.gov\l"
> setwd("C:\Users\lopez235\Local-NOTBackedUp\R Files Local")
Error: '\U' used without hex digits in character string starting "C:\U"

3 个答案:

答案 0 :(得分:16)

在您的路径中使用/代替\

afl.df=read.csv("C:/Users/lopez235/Local-NOTBackedUp/R Files Local/afl_2003_2007.csv")

答案 1 :(得分:3)

当遇到导入数据集的问题时,我更喜欢使用file.choose(),然后手动选择我的文件。 例如:

newdataset <- read.csv(file.choose(), header = T)

要求您手动选择文件的窗口将弹出,header = T(或TRUE)告诉R这些是变量名称。如果您有数据写入标头= FALSE。 如果你想确认现在R知道你可以调用哪些变量名:         名称(newdataset)

答案 2 :(得分:0)

您可以使用switch ($_GET['function_to_call']) { case 0: function1($_GET['id']); // not getting the id's value break; case 1: function2(); break; default: break; } //And your functions. function function1($param) { echo "This is function 1 AND id=".$param; // This string must be passed back to the previous AJAX request. } function function2() { echo "This is function 2"; } 代替\\

\