Skip to content

Instantly share code, notes, and snippets.

@tylerlittlefield
Created February 25, 2021 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylerlittlefield/937d2027ff0be963e77044f632870b27 to your computer and use it in GitHub Desktop.
Save tylerlittlefield/937d2027ff0be963e77044f632870b27 to your computer and use it in GitHub Desktop.
Return a vector of dates on a given day every n weeks
library(lubridate)
library(dplyr)
scheduler <- function(day = "monday", n_weeks = 2) {
x <- data.frame(date = seq.Date(as.Date("2021-01-01"), as.Date("2050-01-01"), by = "day"))
x$week <- ceiling(lubridate::week(x$date) / n_weeks)
x$day <- tolower(as.character(lubridate::wday(x$date, abbr = FALSE, label = TRUE)))
suppressWarnings({
x[x$day %in% day, ] %>%
dplyr::group_by(lubridate::year(date), week, day) %>%
dplyr::summarise(date = min(date), .groups = "drop") %>%
.[["date"]]
})
}
scheduler(day = "monday", n_weeks = 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment