Elegant way to get no of days to prev and next year using R?

北城以北 提交于 2020-07-30 03:29:34

问题


I have an R data frame like as shown below

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"))

I would like to get the no of days to prev year and next year.

I was trying something like the below

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

In python, I know we have something called day of the year and offsets.YearEnd(0) etc which I knew based on this post. But can help me with how to do this using R?

I expect my output to be like as shown below


回答1:


You can use ceiling_date and floor_date from lubridate to get first and last days of the year and then subtract it with date_1 to get days_to_previous_year and days_to_next_year.

library(dplyr)
library(lubridate)

test_df %>%
  mutate(date_1 = mdy(date_1), 
         previous_year = floor_date(date_1, 'year'), 
         next_year = ceiling_date(date_1, 'year') - 1, 
         days_to_previous_year = as.integer(date_1 - previous_year), 
         days_to_next_year = as.integer(next_year - date_1)) %>%
  select(-previous_year, -next_year)


#  subbject_id     date_1 days_to_previous_year days_to_next_year
#1           1 2003-01-01                     0               364
#2           2 2007-12-31                   364                 0
#3           3 2008-12-30                   364                 1
#4           4 2007-01-02                     1               363
#5           5 2007-01-01                     0               364



回答2:


One dplyr and lubridate option could be:

test_df %>%
 mutate(date_1 = mdy(date_1),
        days_to_prev_year = date_1 - mdy(paste0("01-01-", year(date_1))),
        days_to_next_year = mdy(paste0("12-31-", year(date_1))) - date_1)

  subbject_id     date_1 days_to_prev_year days_to_next_year
1           1 2003-01-01            0 days          364 days
2           2 2007-12-31          364 days            0 days
3           3 2008-12-30          364 days            1 days
4           4 2007-01-02            1 days          363 days
5           5 2007-01-01            0 days          364 days


来源:https://stackoverflow.com/questions/62698244/elegant-way-to-get-no-of-days-to-prev-and-next-year-using-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!