Do you need Microsoft Office (Excel) to read .csv files with R? - 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.

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.

How to read in an xls file after using unzip() to place it into a temporary folder?

I receive weekly emails from a database as zip files. In the zip file is a single xls file. When I use unzip() and place the xls file into a designated shared network directory, I cannot use any xls read functions to actually access and manipulate the data (I haven't tried read.xls because of the Perl dependencies, but I'm willing to if there are no other options).
I've tried every Excel reader I can find.
read_xls(unzip(zipfile = zfile, files = "Data Extract.xls", exdir = "~\\Excel Files"))
Errors include the following where I would simply expect a dataframe output:
"libxls error: Unable to open file"
"Error in open.connection(file, "rt") : cannot open the connection
In addition: Warning message:
In open.connection(file, "rt") :
cannot open zip file 'Data.zip:F'
EDIT: It turns out that despite the DB interface calling this file within the zip an .xls file, it's an HTML file and readHTMLTable() from library(XML) did the trick just fine. Thank you for the questions which lead me to looking at ths issue from a different angle.

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.

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.

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