【问题标题】:R lubridate: pretty_dates fails for some input dataR lubridate:pretty_dates 对某些输入数据失败
【发布时间】:2014-07-10 00:33:52
【问题描述】:

我在使用 lubridate 的 pretty_dates 函数时遇到问题。在某些情况下它会失败:

library(lubridate)

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

(我通常不会像这样构造日期时间,这正是dput 给我的。) 这失败并显示错误消息:

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

我在这里做错了吗?

我正在使用lubridate_1.3.3,来自 CRAN 的最新版本。

【问题讨论】:

    标签: r lubridate


    【解决方案1】:

    我认为这看起来像一个错误。当您有一个 POSIX 对象的时间不是午夜并且首选“天”单位时,这似乎只是一个问题。您可以使用其他值重新创建

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

    虽然像

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

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

    工作。

    看起来你可以解决它

    pretty_dates(as.Date(datetimes),10)
    

    【讨论】:

    • 错误源于pretty_dates-end &lt;- pretty.point(max(rng), binunits, binlength, start = FALSE)这一行。如果你删除 start=FALSE 一切正常。
    • 好吧,那时它正在运行pretty.point(max(datetimes), "day", 2, start=F)。再次pretty.point(as.Date(max(datetimes)), "day", 2, start=F) 有效。我认为这与pretty.point 中的ceiling_datefloor_date 有关,它们在有时间时返回不同的日期,而在您执行as.Date 删除时间的同一天。这与之后调用的 seq 混淆了。
    • pretty.point(max(datetimes), "day", 2, start=F) 给我character(0),这破坏了seq 通话。
    • lubridate 错误跟踪器在这里:github.com/hadley/lubridate/issues,如果你想报告这个。
    • 非常感谢!变通方法剥夺了 pretty_dates 其他出色的灵活性,例如,当间隔实际上在一天的某个小时内时。我将其报告为错误github.com/hadley/lubridate/issues/248
    猜你喜欢
    • 1970-01-01
    • 2017-05-23
    • 1970-01-01
    • 2018-11-22
    • 2014-06-24
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    相关资源
    最近更新 更多