0

I am trying to convert an element of a matrix from what were Excel serial dates to a vector of Date objects before using plot().

I can create a vetor and I get the expected result:

    library(chron)

# set date origin as defined in Excel

    options(chron.origin = c(month=1, day=1, year=1900))

    test_dates <- c(40917:40920)

    test_dates
    ## [1] 40917 40918 40919 40920

    chron(test_dates, out.format = "m/d/y")

    ## [1] 01/11/12 01/12/12 01/13/12 01/14/12

But when I try to use this on my actual vector, it does not work

# first 10 vlaues in vector

pivot_pred$date

 ## [1] 40917 40918 40919 40920 40921 40922 40923 40924 40925 40926...

chron(pivot_pred$date, out.format = "m/d/y")

## Error in chron(dates. = floor(dts), times. = tms, format = format, out.format = out.format,  : 
  misspecified chron format(s) length

I'm sure this is simple but I have tried many variations and none worked. Any suggestions on what I'm doing wrong?

eafpres
  • 171
  • 2
  • 9
  • 1
    Hard to say without seeing pivot_pred (You gave test_dates). Try in base R instead: as.Date(pivot_pred$date, origin = "1900-01-01") – Edgar Santos Sep 18 '17 at 04:48
  • Maybe you should try `out.format = c(dates = "m/d/y", times = "h:m:s")` as the second argument to `chron()`. If the problem persist then you should provide `dput(pivot_pred)` as pointed out by @ed_sans – Prem Sep 18 '17 at 06:35
  • @ed_sans: Thank you, your method worked. Also, sorry about the confusion in my post, this was intended to convey the date column: # first 10 vlaues in vector pivot_pred$date ## [1] 40917 40918 40919 40920 40921 40922 40923 40924 40925 40926... I should have posted: 'data.frame': 729 obs. of 2 variables: $ date : num 40917 40918 40919 40920 40921 ... $ bookings: num 144180 147877 153333 155835 158262 ... – eafpres Sep 18 '17 at 20:28
  • Sorry for the format issues in my comment; just getting exposed to the lack of formatting.... – eafpres Sep 18 '17 at 20:32
  • @Prem--thank you for your suggestion. It worked as well; apparently chron was interpreting the series values as datetime. – eafpres Sep 18 '17 at 20:39

0 Answers0