首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获取R中月份的最后一个工作日的日期

问如何获取R中月份的最后一个工作日的日期
EN

Stack Overflow用户
提问于 2018-01-05 20:08:10
回答 3查看 4.5K关注 0票数 1

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

提前感谢

EN

回答 3

Stack Overflow用户

发布于 2018-01-05 20:27:47

你需要一个工作日计算器。这里我使用的是来自RQuantLib的一个CRAN包。

给定一个日期,您可以测试它是否是给定(交换)日历中的工作日。然后你只需要根据你想要的时间段进行子集。使用data.table会更容易,但是我在这里让它更简单,这样我们就只依赖于一个包:

代码语言:javascript
复制
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> 
票数 8
EN

Stack Overflow用户

发布于 2018-01-06 04:44:30

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

代码语言:javascript
复制
# 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"
票数 0
EN

Stack Overflow用户

发布于 2019-10-11 11:18:15

考虑到周末和节假日之间的相互作用,这一点很难向量化和快速进行,即

  • 你临近月末的假期可能会把你推入接近月末的周末
  • 一个落在月末的周末可能会把你推入接近月末的假期

代码语言:javascript
复制
# 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)

}

然后,只需向该函数传递您想要的任何日期和假日。

以下是一些测试:

代码语言:javascript
复制
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"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48113346

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档