Can't read CSV file with R script - r

I'm still very new to R and I'm trying to read a moderate-size .csv file with a Script, but the script is unable to detect the file.
My code is at follows:
Saint<- read.csv("./Comp1.csv", stringsAsFactors = FALSE)
Saint
I have attempted several ways to indicate the file path. Including
path<- file.path("desktop", "saint", "Comp1.csv"
Then using the path variable in the read.csv function.
Also explicitly indicating the full path with forward slashes
It always throws the following error
> Saint<- read.csv("./Comp1.csv", stringsAsFactors = FALSE)
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file './Comp1.csv': No such file or directory
>
> Saint
Error: object 'Saint' not found
Any ideas on what might be wrong here?

Related

Library of alternativeFunction() in R

I am trying to read 2 different types of files, using the alternativeFunction(). So if the first file in .txt format could not be found, I could try with the second type in .csv format, but R tells me that the function is not found. Does anyone know the library I have to install?
File <- try(read.table('File.txt',sep=c(',','|'), header=TRUE))
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'File.txt': No such file or directory
if("try-error" %in% class(File)) alternativeFunction(File <- read_csv('File.csv'))
Error in alternativeFunction(File <- read_csv("File.csv")) :
could not find function "alternativeFunction"

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

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.

Importing a CSV file into R

I am Using a MACbook and new to R. I have the social network file on my desktop and I'm trying to get R to read it. I saved it as CSV
command typed:
sn.csv <- read.csv("C:\Users\Opemipo Akinosun\Desktop\social_network.csv", header = T)
I keep getting the following errors:
error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'C:/Users/Opemipo Akinosun/Desktop/social_network.csv': No such file or directory
the resource I'm learning with doesn't ask me to set my directory so I'm a little reluctant to doing that as suggested
You should use file.path that is OS-independant :
ff <- file.path("C:","Users","Opemipo Akinosun","Desktop","social_network.csv")
Then you can check if the file exists:
> file.exists(ff)
[1] FALSE
I worked and I could resolve the issue I was facing. Try changing the directory and then use the above code. So it will be as below
setwd("C:/Users/user/Desktop")
sn <- read.csv("social_network.csv" , header = TRUE)
sn
Run the above command and you should be all set to work further.

Resources