Created
February 25, 2021 18:53
-
-
Save tylerlittlefield/937d2027ff0be963e77044f632870b27 to your computer and use it in GitHub Desktop.
Return a vector of dates on a given day every n weeks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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