Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot export data to a file in R (write.csv)

Tags:

I am trying to export data in R to a csv file, and as much simple as I try to do it, I always get the same error message. Example:

I create a simple data vector to export

 x <- c(1,3,4,3,5,7,5,8,2,5,7)

I try to export with:

write.csv(x,file='whatever.csv')

And I get an error:

error in file(file ifelse (append a w )) cannot open the connection
In addition: Warning message: In file(file, ifelse(append, "a", "w")) :
cannot open  file 'whatever.csv': Permission denied

How can I solve this?

like image 506
Golan_trevize Avatar asked Jul 22 '13 02:07

Golan_trevize


People also ask

Can you export R output?

There are two ways of exporting data into text files through R. One is using the base R functions and another one is using the functions from the readr package to export data into text/CSV format.

Can you export a dataset from R to Excel?

Note that you can export data from R to several formats, like CSV, SAV, XLS, XLSX, TXT or even XML.


2 Answers

First part is to check the working directory and ensure that you have write access to that directory. You can check this with getwd(). I can reproduce your error by trying to write to a directory which is read only.

To set the working directory to something else with read access you can type setwd("H:/foo"). Once you have write access the write.csv(x,file='whatever.csv') should work.

like image 105
Soid Avatar answered Sep 19 '22 13:09

Soid


I got the same issue today and I know I have full permission to the folder. What worked for me is giving it the absolute path.

write.csv(data, file="C:/project/file.csv")

like image 35
Zerihun Bekele Avatar answered Sep 21 '22 13:09

Zerihun Bekele