lebelinoz
lebelinoz

Reputation: 5068

What is the correct date format for writexl

What is the correct date format for the new writexl package? I tried it on lubridate dates, and the resulting Excel spreadsheet contains strings of the form yyyy-mm-dd (i.e. not Excel dates).

Upvotes: 2

Views: 1608

Answers (2)

www
www

Reputation: 4224

The purpose of the writexl package is to essentially create a mirror image of an R table in an excel file format. So having data in one of the usual R date/time formats like as.Date, as.POSIXct, etc. won't translate to a date format shifting from YYYY-mm-dd to d/m/y while being exported to excel. If you'd like it in a more standard excel date/time format in the excel file, it's best to convert it prior to exporting it with something like the strftime() function, like this:

require(writexl)

write_xlsx(
  data.frame(date=strftime(c("2017-09-10","2017-09-11","2017-09-12"),"%d/%m/%y")),
  "~/Downloads/mydata.xlsx")

Output (in xlsx file):

date
10/09/17
11/09/17
12/09/17

Edit:

If you'd like the data to be an Excel date format once it's in the new file, then adding as.POSIXct() to the above will ensure that.

Upvotes: 1

lebelinoz
lebelinoz

Reputation: 5068

It worked when I converted my dates to POSIXct dates using as.POSIXct.

Upvotes: 0

Related Questions