[R] Weekdays

Whit Armstrong Whit.Armstrong at tudor.com
Fri Feb 22 19:31:45 CET 2002


I'm trying to write a function that returns the number of weekdays between a
vector of start dates and a vector of end dates.

Subtracting the 2 times the number of whole weeks is the easy part and works
if the number of days is a multiple of 7.

However, the number of weekend days in the tail is a little harder.  It
depends on both the start date of the tail and the number of days in the

Any suggestions for a more elegant solution would be helpful.

Whit Armstrong

bdays <- function(sdate,edate)
	sdate <- as.POSIXlt(sdate)
	edate <- as.POSIXlt(edate)

	length <- as.integer(difftime(edate,sdate))

	weeks <- floor(length/7)
	tail <- length%%7

	# true no matter what day of the week the start date is
	weekend.days <- weeks*2

	weekend.days <- weekend.days + # number of weekend days in tail


r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list