Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing the first business day of a Month

I need to know the first business day of a given month, is there some package in R containing a relevant function?

like image 606
Lorenzo Rigamonti Avatar asked Dec 01 '25 11:12

Lorenzo Rigamonti


1 Answers

The timeDate package has a function isBizday that will help you here. There will be more elegant ways to convert the dateTime object to other formats, but this should at least get you started.

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" 
like image 77
Josh O'Brien Avatar answered Dec 03 '25 04:12

Josh O'Brien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!