Error while reading csv file

2019-09-23 13:39发布

问题:

I have a xlsx file and to read from Rstudio i saved it as .csv file. Now when i try to read the file from Rstudio, receiving the below error.

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

回答1:

As already mentioned in the comments, the "cannot open the connection" part of the error message confirms that where R is looking is not where the file is.

A few things to keep in mind

  • use getwd() to see the current R working directory
  • use setwd() to change R's wd
  • You should use RStudio projects to organize your projects - this helps with the working directory thing
  • spaces in paths are difficult sometimes. Looks like you have "D:/DATA SCIENCE", that space can cause problems (you may need to escape the space like "DATA\ SCIENCE".
  • You can read the file using the full path (e.g. read.csv("D:/DATA SCIENCE/path/file.csv")) or if you are doing this a lot, set a variable to your path data_path ="D:/DATA SCIENCE/path/" and then read.csv(file.path(data_path, "file.csv")) where file.path concatenates with your OS specific path delimiter.
  • You can import files into RStudio using the Import Dataset option under Tools in the menu bar.


标签: r read.csv