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

时间:2022-10-16 09:56:17

I need to get the last businessday of the previous month from the current date with R (e.g. today is 5th of January 2018, so I should get 29th of December 2017 as a result)

我需要从当前日期的R开始上个月的最后一个工作日(例如今天是2018年1月5日,因此我应该在2017年12月29日获得结果)

Thanks in advance

提前致谢

2 个解决方案

#1


4  

You need a business day calculator. Here I use one from RQuantLib which is a CRAN package.

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

Given a date, you can test if it is a business day in a given (exchange) calendar. Then you just subset by the time period you want. This would be easier with, eg, data.table but I kept it simpler here so that we depend only on one package:

给定日期,您可以测试它是否是给定(交换)日历中的工作日。然后,您只需按您想要的时间段进行分组。这对于例如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> 

#2


0  

You can write your own function to find out last working days. You need to define off days of a week to take decision.

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

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

#1


4  

You need a business day calculator. Here I use one from RQuantLib which is a CRAN package.

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

Given a date, you can test if it is a business day in a given (exchange) calendar. Then you just subset by the time period you want. This would be easier with, eg, data.table but I kept it simpler here so that we depend only on one package:

给定日期,您可以测试它是否是给定(交换)日历中的工作日。然后,您只需按您想要的时间段进行分组。这对于例如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> 

#2


0  

You can write your own function to find out last working days. You need to define off days of a week to take decision.

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

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