Fail to find files in directory, can not find file - r

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

Related

Error in file(file, "rt")

I'm using Rstudio and I cannot open several file .txt because of this error message :
Error in file(file, "rt") : cannot open the connection
I enter :
X1= read.table("fileX1.txt", header = FALSE)
I don't understand because some of my colleagues use the same function with the same .txt file and it works for them
Since you aren't specifying the path to the file, R will look for it in the current directory. If that isn't the directory containing the file, you'll get that error.
You can use setwd() (or a menu command in RStudio) to set the current directory. Alternatively, you could use file.choose() to get a dialog to choose the file; it will return the full filename including the path.

in file(file, "rt") : cannot open the connection In addition: Warning message: In file(file, "rt") : cannot open file: No such file or directory

I am trying to upload a file of csvs and other documents into R studio. My code looks like the following.
setwd("~/Dropbox/R")
getwd()
[1] "/Users/me/Dropbox/R"
myData <-read.csv("~/Dropbox/Exercise_Files")
The message returned is:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file '/Users/me/Dropbox/R/Exercise_Files': No such file or
directory
I can't understand why R is telling me no such directory when getwd() shows the directory I am using. Nor do I understand why I am being told that there are no such files--I put them into the directory! They are right there! Please help; I have tried everything.
You have to specify the .csv extension:
myData <-read.csv("~/Dropbox/Exercise_Files.csv")
list.files() worked for me. I discovered that all my text files were being seen in R as filename.txt.txt but were seen as filename.txt in the Windows file manager. I changed the files to filename (without '.txt') in Windows and now they read into R correctly.

R cannot open a connection

I am trying to use RTextTools for none .csv files.
data7 <- read_data('/Users/Ziegler1812/Desktop/Essays/Fall 2016/461/Data-PrelimforR/Data-PrelimforR4/', type = 'folder',index = '1.txt',1)
Error in file(file, "rt") : cannot open the connection In addition:
Warning message: In file(file, "rt") : cannot open file '1.txt': No
such file or directory
'1.txt' does in fact exist in that location, the problem is not the file's existence, it is getting to the location of the file.
The problem was that in RTextTools the index is not to be an index from within the folder, it is to either be the folder itself, or an outside entity that is similar to that within the folder. Further, the explanation shows that folder is for txt files, however 'delim' is for txt files, which is why I couldn't get it to work.
Thanks.

R – first time using projects – does not open csv file even if it is in working directory

I created a project in R (my first "project"; before I was just dabbling in a moltitude of files). To do that, first I created a new empty folder in Finder and named it "new_study_results_analysis_R". Then I created the new project referring to that folder. After that, I moved all the files I wanted to analyze in the aferomentioned folder.
At this point I tried to open one of the files:
> dat = read.csv("30sub8a10.csv", header = TRUE)
But I get the error:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file '30sub8a10.csv': No such file or directory
So I checked:
> getwd()
[1] "/Users/S/Users/S/Dropbox/(0.01)__OculusStudy/(0.0)New_study/new_study_results_analysis_R"
The working directory is the right one.
I don't understand. How can I open the files?
There's a duplicate "/Users/S/" in the getwd() output. To set the right working directory in a Mac, open up a terminal console, and drag and drop the folder with the data into terminal. Copy the path that appears in terminal and use that in setwd("...").

list.files error (easy?) [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?

Resources