importing a CSV file - r

I just started learning R which is my first programming language. I tried importing a CSV file from my system which is on my downloads in my computer using:
getwd()
setwd("/downloads")
statesInfo <- read.csv('stateData.csv')
But i keep getting the error message below.
setwd("/downloads")
Error in setwd("/downloads") : cannot change working directory
statesInfo <- read.csv('stateData.csv')
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'stateData.csv': No such file or directory.
Could I be getting it wrong.

R is unable to recognize the directory that you're referring to and that's why you get the first error.
The working directory remains unchanged and R can not find the file stateData.csv in the current working directory which results in the second error message.
For setting a working directory use the full path instead: setwd("C:/Users/yourname/Downloads/") - notice the forward slash instead of the back-slash.
However, If you're only importing files, you do not need to change your working directory every time. You can simply refer to files in other locations. If you're using windows - you will need to use ./ for sub-folders and ../ for folders that are one level up. For example. if your working directory is set to 'C:/Users/yourname/Desktop/R' and you want to read a file from the 'Downloads' folder, simply use the below code:
dat <- read.csv("../../Downloads/stateData.csv")
the first ../ takes you one level up to the 'Desktop' and the second ../ takes you to 'Users'. From there you are referring to 'Downloads' folder where the stateData.csv file is located.
EDIT
The above works for Windows isntallations, for Mac/others you would have to use the tilda notation: e.g. ~/Desktop

So, it should be like this.
setwd("c:/mydir")
Also.
MyData <- read.csv(file="c:/mydir/TheDataIWantToReadIn.csv", header=TRUE, sep=",")

Related

loading a .csv file to R

I am learning R but I am stuck in loading my own dataset into R. So far, the code that I have is the following. The dataset (.csv) is located in my desktop.
Data500 <- read.csv("sp500.csv", header = TRUE)
I get this error message in the console.
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'sp500.csv': No such file or directory
This is because the file isn't located in your working directory, which is where R is searching.
use getwd() to see what working directory you are in currently (for this R session).
You can either
a) move your csv file to that working directory (if it happens to be a good place to keep it),
b) specify the full path (e.g., read.csv("C:/Users/DChavez/Documents/Project_Name/sp500.csv"))
OR
c) you can change the working directory to be where your file is before running read.csv().
Two ways to do this:
Use setwd() at the top of your code to set the working directory. In Windows, it might look something like this: setwd("C:/Users/DChavez/Documents/Project_Name/")
IF you are using RStudio, you can navigate to the "Session" bar at the top, then "Set Working Directory" then "Choose Directory...", where you will navigate to the folder that houses your csv file.

Error in openning a file in R with two different method

I have trouble opening a file in R with the following for loop :
#Counting the number of files in the folder
num_files <- length(list.files("C:/Users/Jane/Downloads/WantedFolder"))
num_files
#File extraction from the folder
a<-list.files("C:/Users/Jane/Downloads/Folder")
for (i in 1:num_files){
g<-a[i]
data1<-read.table(g)
head(data1)
}
To me it should be working as the classic read.table :
data1<-read.table("C:/Users/Jane/Downloads/WantedFolder/WantedFile.txt")
I got the following error and don't understand why since I've checked the directory several times and it is working for the "classic" read.table
Warning in file(file, "rt") :
cannot open file 'WantedFile.txt': No such file or directory
Error in file(file, "rt") : cannot open the connection
What is wrong ?
It depends on your working directory, which you can check using getwd(). list.files only returns the names of the files in a folder, not their full path. Based on the error, it seems like you are trying to open a file called "wantedfile.txt" in your current working directory. To avoid R assuming your current working directory, you can specify the absolute path like so:
path <- "C:/Users/Jane/Downloads/Folder"
a <- list.files(path)
a <- file.path(path, a)
This pastes the absolute path of the wanted folder to the file names returned by list.files-

Read() function in R is looking at wrong folder

When I'm calling any read.___() function in R it is looking at the wrong file path. I have tried changing the working directory multiple times and it keeps going to the same path. For example I'm trying the following:
setwd("C:/OneDrive/")
read.csv("~/Desktop/Data/file.csv")
I then get the following error:
Error in file(file, "rt") : cannot open the connection In addition:
Warning message: In file(file, "rt") : cannot open file
'C:/OneDrive/Documents/Desktop/Data/file.csv': No such file
or directory
For some reason the read() function adds the documents folder in place of the specified working directory. I tried uninstalling and deleting all files related to R on my computer then reinstalling. I still get the same issue. I've seen others ask a similar question, but everyone keeps telling them to set the working directory to something different. It doesn't matter what I set the working directory to. R keeps adding the Documents folder into the path. How do I get it to stop adding the Documents folder?

Set wd in RStudio

I am creating a series of r scripts that will be used by multiple people, meaning that the working directory of files used and stored will differ. There are two folders, one for the R code, called "rcode," and another to store the generated outputs, called "data". These two folders will always be shared in tandem. To accommodate for the changing working directory I created a "global" script that has the following lines of code and resides in the "rcode" folder:
source_path = rstudioapi::getActiveDocumentContext()$path
setwd(dirname(source_path))
swd_data <- paste0("..\\data\\")
The first line gets the source path of the global script. The second line makes this the working directory. The third line essentially tells the script to store an output in the "data" folder, which has the same path as the "rcode" folder. So to read in a csv file within the "data" folder I write:
old_total_demand <- read.csv(paste0(swd_data, "boerne_total_demand.csv"))
When I use this script on my Windows laptop it works beautifully, but when I use it on my Mac I get the following error:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file '..\data\demand\boerne_total_demand.csv': No such file or directory
Would anyone have any idea why this would be? Thanks in advance for the help.
I'm not sure what systems your collaborators will be using, but you may run into issues due to differences between Window/Mac/Linux with regards to how paths are written. I suggest you create a R Project .Rprj using RStudio and save that in your directory that contains subdirectories for data and rcode, and share the entire project directory.
/Project_dir/MyProject.Rprj
/Project_dir/data/
/Project_dir/rcode/
Then from the R project opened through RStudio you should be able to directly refer to your data by:
data <- read.csv("data/boerne_total_demand.csv")
The working directory will always be where your .Rproj is stored, so you can avoid having to setwd as it causes lots of chaos when sharing and collaborating with others.
I have this code from my current script at hand.
I hope you like it !
path <- dirname(getActiveDocumentContext()$path)
setwd(path)
swd_path <- paste0(path,"/data/")
if(!dir.exists(swd_path)){
dir.create(swd_path)
}
old_total_demand <- read.csv(paste0(swd_data, "boerne_total_demand.csv"))

Error while reading csv file

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
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.

Resources