Search code examples
rexport-to-csv

How to write a CSV file in R


I am unable to write a to a CSV file in R. I receive two errors. After reviewing other posts I will add that I am an admin on my system. Please can someone explain to me what I am doing wrong. That includes the writing of this post as it is my first. Thank you for your time.

I have set my working directory and received no error:

setwd("~/R_Learning_Files/titanic")

I checked my working directory:

getwd()
"C:/Users/felix/OneDrive/Documents/R_Learning_Files/titanic"

My attempt to write to a CSV file:

submit_df <- data.frame(passenger_id = rep(892:1309, survived = rf_05_preds))

write.csv(submit_df, file = "rf_sub_2021/07/11_01", row.names = FALSE)

The error I have received:

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 'rf_sub_2021/07/11_01': No such file or directory

Solution

  • Don't use / in filenames. Also a csv file should end with '.csv'. Try -

    write.csv(submit_df, file = "rf_sub_2021-07-11_01.csv", row.names = FALSE)