Importing a CSV file into R - 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.

Related

Can't read CSV file with R script

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?

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.

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.

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