rdatedatetimedput

dput(as.Date(df$date)) shows up as number


I have a dataframe df with date column date, df$date:

[1] "2013-01-31" "2013-02-28" "2013-03-31" "2013-04-30" "2013-05-31" "2013-06-30", etc.

When I dput(as.Date(df$date)), it shows up as numbers:

structure(c(15736, 15764, 15795, 15825, 15856, ...), class = "Date")

I also try: dput(as.Date(df$date, origin="1970-01-01")), it returns same result.

How could I get the result as follows:

structure(c("2013-01-31", "2013-02-28", "2013-03-31", "2013-04-30", "2013-05-31", "2013-06-30", ...), class = "Date")

Solution

  • This is a non-issue (unless for some unknown reason you care about how the value is encoded in the dput output), since when you load the dput output, the date is returned. It's just the way the data is coded - that class = "Date" means R can interpret the numerical value as a date.

    structure(c(15736, 15764, 15795, 15825, 15856), class = "Date")
    
    [1] "2013-01-31" "2013-02-28" "2013-03-31" "2013-04-30" "2013-05-31"