Golan_trevize Golan_trevize - 1 month ago 4
R Question

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

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?Thanks a lot.

Answer

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.

Comments