【问题标题】:Floor datetime with custom start time (lubridate)具有自定义开始时间的地板日期时间(润滑)
【发布时间】:2020-01-15 15:57:53
【问题描述】:

有没有办法使用自定义开始时间而不是最早的可能时间来确定日期?

例如,从上午 8 点和下午 8 点开始,而不是从上午 12 点和下午 12 点开始,将一天中的地板小时分为 2 个 12 小时间隔。

例子:

x <- ymd_hms("2009-08-03 21:00:00")
y <- ymd_hms("2009-08-03 09:00:00")
floor_date(x, '12 hours')
floor_date(y, '12 hours')

# default lubridate output:
[1] "2009-08-03 12:00:00 UTC"
[1] "2009-08-03 UTC"

# what i would like to have:
[1] "2009-08-03 20:00:00 UTC"
[1] "2009-08-03 08:00:00 UTC"


【问题讨论】:

    标签: r datetime lubridate


    【解决方案1】:

    你可以编写一个小的switch(不过没有lubridate)。

    FUN <- function(x) {
      s <- switch(which.min(abs(mapply(`-`, c(8, 20), as.numeric(substr(x, 12, 13))))), 
                  "08:00:00", "20:00:00")
      as.POSIXct(paste(as.Date(x), s))
    }
    FUN("2009-08-03 21:00:00")
    # [1] "2009-08-03 20:00:00 CEST"
    
    FUN("2009-08-03 09:00:00")
    # [1] "2009-08-03 08:00:00 CEST"
    

    【讨论】:

    • 谢谢!我会投票,并等待一段时间,看看是否有任何 lubridate 选项是可能/可用的。
    猜你喜欢
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    • 2018-08-15
    • 1970-01-01
    相关资源
    最近更新 更多