datetime - Calculate the number of weekdays between 2 dates in R -
i'm trying write r function calculate number of weekdays between 2 dates. example, nweekdays('01/30/2011','02/04/2011') equal 5.
similar this question. thanks!
/edit: @j. winchester's answer great, wondering if think of way vectorize this, it'll work on 2 columns of dates. thanks! /edit 2: again!
date1 <- as.date("2011-01-30") date2 <- as.date("2011-02-04") sum(!weekdays(seq(date1, date2, "days")) %in% c("saturday", "sunday"))
edit: , zach said, let there vectorize
:)
dates1 <- as.date("2011-01-30") + rep(0, 10) dates2 <- as.date("2011-02-04") + seq(0, 9) nweekdays <- vectorize(function(a, b) sum(!weekdays(seq(a, b, "days")) %in% c("saturday", "sunday"))) nweekdays(dates1, dates2)
Comments
Post a Comment