Find the first and last working day in R or Mysql

I am looking to get a list of the first and last working days of the month.
This is basically a list of business days:

2009-01-03 2009-01-04 2009-01-05 ... 

I just want to get a list of the first and last days, mainly max and min day (date) for each combination of year and month.

Any suggestions?

+4
source share
1 answer

Your question indicates that you already have a list of working days, and you need a way to find the minimum and maximum for each combination of the year and month.

You can use ddply in the plyr package for this. I also use the lubridate package because it has some handy features for retrieving the year and month from a date.

Create some data:

 library(lubridate) x <- sample(seq(as.Date("2011-01-01"), by="1 day", length.out=365), 100) df <- data.frame(date=x, year=year(x), month=month(x)) 

Now extract min and max for each month:

 library(plyr) ddply(df, .(year, month), summarize, first=min(date), last=max(date)) year month first last 1 2011 1 2011-01-03 2011-01-30 2 2011 2 2011-02-03 2011-02-19 3 2011 3 2011-03-06 2011-03-29 4 2011 4 2011-04-09 2011-04-30 5 2011 5 2011-05-01 2011-05-29 6 2011 6 2011-06-04 2011-06-28 7 2011 7 2011-07-02 2011-07-29 8 2011 8 2011-08-10 2011-08-30 9 2011 9 2011-09-01 2011-09-28 10 2011 10 2011-10-07 2011-10-31 11 2011 11 2011-11-01 2011-11-28 12 2011 12 2011-12-01 2011-12-30 
+6
source

Source: https://habr.com/ru/post/1369176/


All Articles