Marc van der Peet
Marc van der Peet

Reputation: 343

Cant create file name with time stamp

I have a file that I would to write away in a certain dir. Therefore I have the following code:

 function <- {

   file_path_new <- file.path("C:", "Users", "MavanderPeet", "Documents", "data")
   setwd(file_path_new)

  now <- Sys.time()
  file_name <- paste0(now, "data_set.csv")
  write.csv(data_frame, file_name)
  # write.csv(data_frame, "file.csv") #for checking purposes

 }

The part where I want to create a name with timestamp does not seem to work however... When I uncomment the line

 write.csv(data_frame, "file.csv")

Everything works fine. So I guess it should be a syntax error....

Any thoughts??

Upvotes: 9

Views: 9883

Answers (2)

Cowpu2
Cowpu2

Reputation: 51

In the answer by @Roland, now needs parenthesis:

paste0(format(now(), "%Y%m%d_%H%M%S_"), "data_set.csv")

Upvotes: 3

Roland
Roland

Reputation: 132706

The colon (:) is not allowed in Windows file names (reference).

Use a different format:

paste0(format(now, "%Y%m%d_%H%M%S_"), "data_set.csv")

Upvotes: 15

Related Questions