-
Notifications
You must be signed in to change notification settings - Fork 4
/
wom.R
31 lines (26 loc) · 1012 Bytes
/
wom.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#' Generate Week of the Month from a Date
#'
#' @description \code{wom()} is a convenience function I use for constructing
#' calendars in \pkg{ggplot2}. It takes a date and returns, as a numeric
#' vector, the week of the month for the date given to it.
#'
#' @details \code{wom()} assumes Sunday is the start of the week. This can
#' assuredly be customized later in this function, but right now the assumption
#' is Sunday is the start of the week (and not Monday, as it might be in
#' other contexts).
#'
#' @param x a date
#' @return \code{wom()} is a convenience function I use for constructing
#' calendars in \pkg{ggplot2}. It takes a date and returns, as a numeric
#' vector, the week of the month for the date given to it.
#'
#' @examples
#'
#' wom(as.Date("2022-01-01"))
#'
#' wom(Sys.Date())
wom <- function(x) {
first <- as.Date(paste(format(x, "%Y"), format(x, "%m"), "01", sep="-"))
first <- as.numeric(format(first, "%w"))+1
return((as.numeric(format(x, "%d")) +(first-2)) %/% 7+1)
}