读取 csv 文件时出错
Error while reading csv file
我有一个 xlsx 文件,为了从 Rstudio 读取,我将其保存为 .csv 文件。现在,当我尝试从 Rstudio 读取文件时,收到以下错误。
setwd("D:/DATA SCIENCE/CCPP-Linear regression")
ccpp<- read.csv("Folds5x2_pp.csv", header = TRUE)
Error in file(file, "rt") : cannot open the connection In addition:
Warning message: In file(file, "rt") : cannot open file
'Folds5x2_pp.csv': No such file or directory
正如评论中已经提到的,错误消息的 "cannot open the connection" 部分确认 R 所查找的位置不是文件所在的位置。
一些注意事项
- 使用
getwd()
查看当前R工作目录
- 使用
setwd()
改变R的wd
- 您应该使用 RStudio 项目来组织您的项目 - 这有助于处理工作目录
- space路径中的内容有时很困难。看起来你有 "D:/DATA SCIENCE",space 可能会导致问题(你可能需要像
"DATA\ SCIENCE"
. 那样转义 space
- 您可以使用完整路径(例如
read.csv("D:/DATA SCIENCE/path/file.csv")
)读取文件,或者如果您经常这样做,请为您的路径设置一个变量 data_path ="D:/DATA SCIENCE/path/"
然后 read.csv(file.path(data_path, "file.csv"))
其中file.path
与您的 OS 特定路径分隔符连接。
- 您可以使用菜单栏中“工具”下的“导入数据集”选项将文件导入 RStudio。
我有一个 xlsx 文件,为了从 Rstudio 读取,我将其保存为 .csv 文件。现在,当我尝试从 Rstudio 读取文件时,收到以下错误。
setwd("D:/DATA SCIENCE/CCPP-Linear regression")
ccpp<- read.csv("Folds5x2_pp.csv", header = TRUE)
Error in file(file, "rt") : cannot open the connection In addition: Warning message: In file(file, "rt") : cannot open file 'Folds5x2_pp.csv': No such file or directory
正如评论中已经提到的,错误消息的 "cannot open the connection" 部分确认 R 所查找的位置不是文件所在的位置。
一些注意事项
- 使用
getwd()
查看当前R工作目录 - 使用
setwd()
改变R的wd - 您应该使用 RStudio 项目来组织您的项目 - 这有助于处理工作目录
- space路径中的内容有时很困难。看起来你有 "D:/DATA SCIENCE",space 可能会导致问题(你可能需要像
"DATA\ SCIENCE"
. 那样转义 space
- 您可以使用完整路径(例如
read.csv("D:/DATA SCIENCE/path/file.csv")
)读取文件,或者如果您经常这样做,请为您的路径设置一个变量data_path ="D:/DATA SCIENCE/path/"
然后read.csv(file.path(data_path, "file.csv"))
其中file.path
与您的 OS 特定路径分隔符连接。 - 您可以使用菜单栏中“工具”下的“导入数据集”选项将文件导入 RStudio。