该TIMEDATE封装具有的功能isBizday,这将帮助你在这里.将dateTime对象转换为其他格式会有更优雅的方法,但这至少应该让你开始.
library(timeDate)
## Example data
dates <- as.Date("2013-01-01") + 0:364
Dates <- as.timeDate(dates)
## Extract the first business day of each month
bizDates <- dates[isBizday(Dates, holidays=holidayLONDON())]
firsts <- tapply(bizDates, months(bizDates), min)
sapply(firsts, function(X) as.character(as.Date(X)))
# 1 2 3 4 5 6
# "2013-01-02" "2013-02-01" "2013-03-01" "2013-04-01" "2013-05-01" "2013-06-03"
# 7 8 9 10 11 12
# "2013-07-01" "2013-08-01" "2013-09-03" "2013-10-01" "2013-11-01" "2013-12-02"
Run Code Online (Sandbox Code Playgroud)
假设您想要的不是周六或周日的第一天:
businessDay<-function(month,year){
#3 first dates of the month is sufficient
d <- as.POSIXlt(paste(year,month,1:3,sep="-"))
#POSIXlt object contains the info about the weekday as number 0-6 (starting Sunday)
d[which(d$wday>0 & d$wday<6)[1]]
}
businessDay(3,2013)
[1] "2013-03-01"
Run Code Online (Sandbox Code Playgroud)
或者如果你想要当天的名字:
businessDay<-function(month,year){
d <- as.POSIXlt(paste(year,month,1:3,sep="-"))
weekdays(d[which(d$wday>0 & d$wday<6)[1]])
}
businessDay(1,2013)
[1] "friday"
Run Code Online (Sandbox Code Playgroud)
你可以使用isBusinessDay from RQuantLib,检查一天是否是一个日历的商业日.一个想法是给出本月的第一天,并采取最小的商业日
例如,2009年4月的第一个工作日是:
library(RQuantLib)
dates <- seq(from=as.Date("2009-04-01"), to=as.Date("2009-04-05"), by=1)
min(dates[isBusinessDay("UnitedKingdom", dates)])
"2009-04-01"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3637 次 |
| 最近记录: |