Mark
Mark

Reputation: 1769

Add actual date to a csv output file name

I am exporting data frame from R to a csv file. I'm using the statement

write.csv(df, file = "df_2019-12-23.csv", row.names =FALSE)

The file name contains the actual date (2019-12-23) but I would like to add this information by a command (and not manually as in my example).

Thank you.

Upvotes: 0

Views: 2597

Answers (3)

MidderC
MidderC

Reputation: 1

Sotos is correct, however for a more beginner approach i would go with:

CurrentDate <- Sys.Date()
write.csv(data, paste0("datafilename",CurrentDate,".csv"), row.names = FALSE)

Upvotes: 0

Sotos
Sotos

Reputation: 51592

You can use Sys.Date() to get the current day's date, i.e.

paste0('df_', Sys.Date(), '.csv')

Upvotes: 4

hello_friend
hello_friend

Reputation: 5788

Base R to write out csv that's name is the name of the dataframe concatenated with today's date (on your systems timezone), in the current working directory:

write.csv(df,
          file = paste0(file.path(
            getwd(), gsub("[[:punct:]]|\\s+", "_",
                          paste0(deparse(substitute(
                            df
                          )), "_",
                          Sys.Date())), ".csv"
          )), row.names = FALSE)

Upvotes: 2

Related Questions