【问题标题】:Get Date objects back from min(dates):max(dates) in R?从 R 中的 min(date):max(dates) 获取 Date 对象?
【发布时间】:2022-07-16 00:35:40
【问题描述】:

一些R代码:

> dates <- as.Date(c('2020-01-01', '2020-01-02'))
> min(dates)
[1] "2020-01-01"
> max(dates)
[1] "2020-01-02"

> min(dates):max(dates)
[1] 18262 18263
> as.Date(min(dates):max(dates))
Error in as.Date.numeric(min(dates):max(dates)) : 
  'origin' must be supplied
> as.Date(min(dates):max(dates), origin="1970-01-01")
[1] "2020-01-01" "2020-01-02"

这表明minmax 正在按预期工作,但是当我将它们放在一个范围内时,日期会变成整数。我该如何预防?

我可以只使用“原点”,但它似乎是一个 hack。

【问题讨论】:

    标签: r date


    【解决方案1】:

    不要使用:,而是使用seq,它已经有Date类的方法

    seq(min(some_dates), max(some_dates), by = "1 day")
    

    【讨论】:

      猜你喜欢
      • 2019-06-25
      • 2019-08-30
      • 2020-06-18
      • 1970-01-01
      • 1970-01-01
      • 2020-11-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      相关资源
      最近更新 更多