使用 R 中的 lubridate 包查找一年中的某一天
Find day of year with the lubridate package in R
我正在寻找具有 lubridate 的 POSIXct class 对象在一年中的哪一天。例如,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
有没有一种简单的方法可以在一年中的某一天这样做?我已经搜索了文档和其他问题,但(还)没有找到答案。
正确的函数是yday
,如
lubridate::yday(Sys.time())
在从 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())
我正在寻找具有 lubridate 的 POSIXct class 对象在一年中的哪一天。例如,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
有没有一种简单的方法可以在一年中的某一天这样做?我已经搜索了文档和其他问题,但(还)没有找到答案。
正确的函数是yday
,如
lubridate::yday(Sys.time())
在从 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())