Count The Mondays in a Time Interval with Lubridate

November 25, 2015
By

(This article was first published on The Lab-R-torian, and kindly contributed to R-bloggers)

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.

library(lubridate)
startDate <- dmy("21-May-2015")
endDate <- dmy("19-Aug-2015")

Now build a sequence between the dates:

myDates <-seq(from = startDate, to = endDate, by = "days")
head(myDates)

## [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.

wday(startDate)

## [1] 5

This means that 2015-05-21 was a Thursday. To get the abbreviation, you can enter:

wday(startDate, label = TRUE)

## [1] Thurs
## Levels: Sun < Mon < Tues < Wed < Thurs < Fri < Sat

and to get the full name of the day:

wday(startDate, label = TRUE, abbr = FALSE)

## [1] Thursday
## 7 Levels: Sunday < Monday < Tuesday < Wednesday < Thursday < ... < Saturday

Leap years are accounted for:

wday(dmy("29-Feb-1504"), label = TRUE, abbr = FALSE)

## [1] Monday
## 7 Levels: Sunday < Monday < Tuesday < Wednesday < Thursday < ... < Saturday

So, we can use this as follows to find the Mondays:

which(wday(myDates)==2)

##  [1]  5 12 19 26 33 40 47 54 61 68 75 82 89

So the whole code to count them is:

startDate <- dmy("21-May-2015")
endDate <- dmy("19-Aug-2015")
myDates <-seq(from = startDate, to = endDate, by = "days")
length(which(wday(myDates)==2))

## [1] 13

I was born on August 04, 1971. This was a Wednesday. How many Wednesdays since I was born?

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] 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

Teach us to number our days, that we may gain a heart of wisdom. Psalm 90:12.

To leave a comment for the author, please follow the link and comment on their blog: The Lab-R-torian.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)