Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R lubridate: pretty_dates fails for some input data

Tags:

r

lubridate

I am having trouble using lubridate's pretty_dates function. It fails in some cases:

library(lubridate)

datetimes <- structure(c(1391640346, 1393856900), class = c("POSIXct", "POSIXt"), tzone = "GMT")
pretty_dates(datetimes, 10)

(I normally wouldn't construct datetimes like this, it's just what dput gave me.) This fails with error message:

Error in seq.POSIXt(start, end, paste(binlength, binunits)) : 'to' must be of length 1

Am I doing something wrong here?

I am using lubridate_1.3.3, the latest version from CRAN.

like image 335
Bernd Avatar asked Nov 10 '22 05:11

Bernd


1 Answers

I believe that looks like a bug. It seems to only be a problem when you have a POSIX object that has a time other than midnight and a prefered units of "days". You can re-create with other values with

pretty_dates(seq.POSIXt(as.POSIXct("2014-02-05 01:00:00 GMT"), 
    by = "5 day", length.out = 2), 2)

while something like

pretty_dates(seq.POSIXt(as.POSIXct("2014-02-05 01:00:00 GMT"), 
    by = "9 hours", length.out = 2), 2)

or

pretty_dates(seq.POSIXt(as.POSIXct("2014-02-05 01:00:00 GMT"), 
    by = "3 months", length.out = 2), 2)

work.

Looks like you can work around it with

pretty_dates(as.Date(datetimes),10)
like image 138
MrFlick Avatar answered Nov 15 '22 07:11

MrFlick