Recently, while working on quantifying the inpatient workload volume of routine tests as a function of the day of the week, I needed to be able to count the number of Mondays, Tuesdays, etc in a time–interval so I could calculate the average volume for each weekday in a time–interval.
The lubridate package makes this a very easy thing to do. Suppose the first date in your series is 21-May-2015 and the last date is 19-Aug-2015.
1 2 3 4 |
library(lubridate) startDate <- dmy("21-May-2015") endDate <- dmy("19-Aug-2015") |
Now build a sequence between the dates:
1 2 3 |
myDates <-seq(from = startDate, to = endDate, by = "days") head(myDates) |
1 2 |
## [1] "2015-05-21 UTC" "2015-05-22 UTC" "2015-05-23 UTC" "2015-05-24 UTC" ## [5] "2015-05-25 UTC" "2015-05-26 UTC" |
The function wday()
tells you which weekday a date corresponds to with Sunday being 1, Monday being 2 etc.
1 2 |
wday(startDate) |
1 |
## [1] 5 |
This means that 2015-05-21 was a Thursday. To get the abbreviation, you can enter:
1 2 |
wday(startDate, label = TRUE) |
1 2 |
## [1] Thurs ## Levels: Sun < Mon < Tues < Wed < Thurs < Fri < Sat |
and to get the full name of the day:
1 2 |
wday(startDate, label = TRUE, abbr = FALSE) |
1 2 |
## [1] Thursday ## 7 Levels: Sunday < Monday < Tuesday < Wednesday < Thursday < ... < Saturday |
Leap years are accounted for:
1 2 |
wday(dmy("29-Feb-1504"), label = TRUE, abbr = FALSE) |
1 2 |
## [1] Monday ## 7 Levels: Sunday < Monday < Tuesday < Wednesday < Thursday < ... < Saturday |
So, we can use this as follows to find the Mondays:
1 2 |
which(wday(myDates)==2) |
1 |
## [1] 5 12 19 26 33 40 47 54 61 68 75 82 89 |
So the whole code to count them is:
1 2 3 4 5 |
startDate <- dmy("21-May-2015") endDate <- dmy("19-Aug-2015") myDates <-seq(from = startDate, to = endDate, by = "days") length(which(wday(myDates)==2)) |
1 |
## [1] 13 |
I was born on August 04, 1971. This was a Wednesday. How many Wednesdays since I was born?
1 2 3 4 5 |
startDate <- dmy("04-Aug-1971") endDate <- dmy("25-Nov-2015") myDates <-seq(from = startDate, to = endDate, by = "days") length(which(wday(myDates, label = TRUE)=="Wed")) |
1 |
## [1] 2313 |
Which means, today I am 2312 weeks old today! Hurray. This is not a typo. The time interval is flanked by Wednesdays so there is one more Wednesday than the number of weeks in the interval. I thank my first–year calculus prof for beating this into me with reference to Simpson's Rule numerical integration.
Hope that comes in handy.
-Dan