使用R获得上一年和明年的日子的绝妙方法? [英] Elegant way to get no of days to prev and next year using R?

查看:113
本文介绍了使用R获得上一年和明年的日子的绝妙方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的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 2007年2月2日, 2007年1月1日))

我想获取天数到上一年和明年。


我正在尝试类似以下的方法

  library(lubridate)
test_df $ current_yr = year(mdy(test_df $ date_1))
prev_yr = test_df $ current_yr-1#(减去1得到上一个年份)
next_yr = test_df $ current_yr + 1#(加1得到上一个年份)
days_to_prev_yr = days_in_year(current_yr)#这不起作用

在python,我知道我们有一个叫做一年中的某天 offsets.YearEnd(0)的东西,我知道在此

解决方案

您可以使用 ceiling_date 和从 lubridate 中的 floor_date 获取一年中的第一天和最后一天,然后用 date_1减去获得 days_to_previous_year days_to_next_year

 库(dplyr)
库(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,-ne xt_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 1363
#5 5 2007-01-01 0 364


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

解决方案

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

这篇关于使用R获得上一年和明年的日子的绝妙方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆