list.files error (easy?) [R] - r

Im trying to r.bind all files in certain directory with the following code in R (all files are formatted the same way):
gene_list <- list.files("/nethome/genelist/")
gene_CH <- do.call("rbind",lapply(gene_list, FUN=function(files)
{read.table(files, header=TRUE, sep="\t", stringsAsFactors=FALSE)}))
write.table(gene_CH,"/nethome/genelist/all.genes.CH_v2t.txt",sep="\t",quote=F,row.names=F)
However, 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 'A2ML1_v2t.txt': No such file or directory
Thing is, A2ML1_v2t.txt is in the directory. Also, what is more puzzling (to me anyway) is that this code worked this morning. But for some reason, its not working now.
Any suggestions?

Two possibilities:
This morning, your working directory was /nethome/genelist/, but now, it isn't. list.files() only gives filenames, not absolute paths, so your second line will search through the current working directory. Try getwd() to check and setwd() to change it.
You do have read permission in the directory, do you?

Related

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-

Fail to find files in directory, can not find file

I tried to write my first function and use multiple files, I tried to create a directory
Step1. I create directory: "specdata", I run: getwd()
and get "/Users/wulingqi/Documents/R/coursera"
Step2. I unzip a zip file and move 332 csv files inside to the "specdata" folder
Step3. I run read.csv("1.csv") but get the error:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") : cannot open file '1.csv': No such file or directory
Step4. I searched on stack, and found this error may because of /spectdata/1.csv, which should be /spectdata1.csv
So I tried read.csv("1.csv",sep = "/") but get the same error.
Could someone give me some advice about this error? Thank you
f you changed your wd to ~/Documents/R/coursera/specdata, read.csv("1.csv") should do the job. From Martin Gal

Do you need Microsoft Office (Excel) to read .csv files with R?

I just downloaded R onto my computer, but when I try to read a .csv file 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 '[filename].csv': No such file or directory
This happens both when I command the program to read the .csv or when I try to import it manually (although that error code is slightly different).
Could this be because I don't have Microsoft Office (and subsequently, Excel) on my computer? The .csv file exists and is not corrupt because I have no problem uploading the file to Google sheets and displaying the data there. Either that, or I didn't install R correctly. Any suggestions?
No, you don't need any special packages to read plain text CSV files in R.
Looking at your error message:
Warning message: In file(file, "rt") : cannot open file '[filename].csv': No such file or directory
I would guess that the problem is your path to the file in question. As a quick fix, you can try using the fully qualified path to your file. For example, on Windows you might try this:
data <- read.csv(file="c:/path/to/filename.csv", header=TRUE, sep=",")
If this works, then the issue was the location of your CSV file. Then, if you want to continue using a relative path, you'll have to figure out what that would be given your current R setup.

importing a CSV file

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=",")

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