如何在R中获取每月最后一个工作日的日期

时间:2018-01-05 12:08:10

标签: r date

我需要从R的当前日期获得上个月的最后一个工作日(例如今天是5th of January 2018,因此我应该得到29th of December 2017

提前致谢

3 个答案:

答案 0 :(得分:5)

您需要一个营业日计算器。在这里,我使用来自RQuantLib的CRAN软件包。

给定日期,您可以测试它是否是给定(交换)日历中的营业日。然后,您只需按您想要的时间段进行分组。这对于例如data.table会更容易,但我在这里更简单,所以我们只依赖于一个包:

R> library(RQuantLib)
R> dateseq <- seq(as.Date("2017-12-01"), as.Date("2017-12-31"), by="1 day")
R> df <- data.frame(dates=dateseq, bizday=isBusinessDay("UnitedStates", dateseq))
R> tail(df[ df[,"bizday"], ], 1)
        dates bizday
29 2017-12-29   TRUE
R> 
R> tail(df[ df[,"bizday"], "dates"], 1)
[1] "2017-12-29"
R> 

答案 1 :(得分:0)

您可以编写自己的函数来查找最后的工作日。您需要定义一周的off days来做出决定。

# Off days
offdays <- c("Saturday", "Sunday")

# Say you want to find out last working date of December 2017
monthdates <- seq(as.Date("2017-12-01"), as.Date("2017-12-31"), by="1 day")

#Eliminate off days
monthdates<- monthdates[! weekdays(monthdates) %in% offdays]

#Find out max date in vector now
lastworkingday <- max(monthdates)

#> lastworkingday
#[1] "2017-12-29"

答案 2 :(得分:0)

鉴于周末和节假日之间的相互作用,因此很难对此向量进行矢量化和快速制作

  • 您临近月末的假期可能会将您带入临近月末的周末

  • 一个月末的周末可能会将您带入月末附近的假期

# get these packages
 require(lubridate)
 require(tidyverse)


calc_business_monthends<- function(dates = NULL, 
                                   holidays = NULL, 
                                   prior_month = FALSE){

  # terminate early 
  if (length(dates) == 0) return(dates)

  # make dates into prior monthend dates
  in_dates<- rollback(dates)

  # or make dates into current monthend dates
  if(!prior_month) in_dates<- rollback((in_dates + 1) + months(1))


  # inner function to recursively check dates and step backward if monthend falls on a holiday or weekend
  step_back<- function(in_dates, holidays) {

    # correct for Sun or Sat
    out_dates<- 
      case_when(wday(in_dates) == 7 ~ in_dates - 1,
                wday(in_dates) == 1 ~ in_dates - 2,
                TRUE ~ in_dates)

    # correct for holidays
    if(!is.null(holidays)){
      out_dates<- 
        if_else(out_dates %in% holidays, out_dates - 1, false = out_dates)
    }

    # if no weekend or holiday changes, we're done; otherwise recurse
    if(identical(out_dates,in_dates)){

      out_dates

    } else {

      step_back(out_dates, holidays)

    }

  } # inner-function end


  # call inner-function 
  step_back(in_dates, holidays)

}

然后只需将所需的任何日期和假期传递给该函数。

以下是一些测试:

Some example dates:
dates<- seq(ymd(20190105), by='month', length.out = 24)

Some holidays near monthend:
holidays<- ymd(20190527,20200525,20210531,20220530,20230529,20240527)

Output:
> calc_business_monthends(dates, holidays)
 [1] "2019-01-31" "2019-02-28" "2019-03-29" "2019-04-30"
 [5] "2019-05-31" "2019-06-28" "2019-07-31" "2019-08-30"
 [9] "2019-09-30" "2019-10-31" "2019-11-29" "2019-12-31"
[13] "2020-01-31" "2020-02-28" "2020-03-31" "2020-04-30"
[17] "2020-05-29" "2020-06-30" "2020-07-31" "2020-08-31"
[21] "2020-09-30" "2020-10-30" "2020-11-30" "2020-12-31"

> calc_business_monthends(dates, holidays, prior_month = TRUE)
 [1] "2018-12-31" "2019-01-31" "2019-02-28" "2019-03-29"
 [5] "2019-04-30" "2019-05-31" "2019-06-28" "2019-07-31"
 [9] "2019-08-30" "2019-09-30" "2019-10-31" "2019-11-29"
[13] "2019-12-31" "2020-01-31" "2020-02-28" "2020-03-31"
[17] "2020-04-30" "2020-05-29" "2020-06-30" "2020-07-31"
[21] "2020-08-31" "2020-09-30" "2020-10-30" "2020-11-30"