【问题标题】:Find day of year with the lubridate package in R在 R 中使用 lubridate 包查找一年中的某一天
【发布时间】:2016-03-15 08:23:00
【问题描述】:

我正在寻找带有 lubridate 的 POSIXct 类对象的日期。例如,12-9-2015 is day 343

使用 lubridate 可以很容易地找到星期或月份:

> lubridate::wday("2015-12-09 04:27:56 EST", labels = T)
Wed
> lubridate::day("2015-12-09 04:27:56 EST")
9

有没有一种简单的方法可以在一年中的这一天做到这一点?我已经搜索了文档和其他问题,但还没有(还)找到答案。

【问题讨论】:

  • 不需要lubridateformat(Sys.time(), "%j")

标签: r lubridate


【解决方案1】:

正确的函数是yday,如

lubridate::yday(Sys.time())

【讨论】:

    【解决方案2】:

    在从 u/blindjesse 偶然发现这个答案之前,想出了一个更复杂的方法:

    # compute the time interval from the first of the year until now
    YTD = interval(floor_date(now(), unit='year'), now())
    # compute the length of the interval in days, and discard the fractional part
    as.integer(time_length(YTD, "day"))
    

    顺便说一句,u/blindjesse 的回答更简洁:

    lubridate::yday(now())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-27
      • 1970-01-01
      • 2014-05-21
      相关资源
      最近更新 更多