【发布时间】:2020-07-02 22:52:20
【问题描述】:
我有一个如下所示的 R 数据框
test_df <- data.frame("subbject_id" = c(1,2,3,4,5),
"date_1" = c("01/01/2003","12/31/2007","12/30/2008","01/02/2007","01/01/2007"))
我想知道前一年和明年的天数。
我正在尝试类似下面的东西
library(lubridate)
test_df$current_yr = year(mdy(test_df$date_1))
prev_yr = test_df$current_yr - 1 #(subtract 1 to get the prev year)
next_yr = test_df$current_yr + 1 #(add 1 to get the prev year)
days_to_prev_yr = days_in_year(current_yr) # this doesn't work
在 python 中,我知道我们有一些名为 day of the year 和 offsets.YearEnd(0) 等的东西,我基于 post 知道了这些东西。但是可以帮助我如何使用 R 来做到这一点?
我希望我的输出如下所示
【问题讨论】:
标签: r dataframe datetime dplyr lubridate