R/financial_quarter.R

Defines functions financial_quarter

Documented in financial_quarter

#' Find the quarter in a financial year for a given month
#'
#' Returns 1 for April, May, June, 2 for July etc.,
#'
#' @param month Month, 1-12, numeric.
#'
#' @return Quarter, 1-4, numeric
#' @export

financial_quarter <- function(month) {

  # Sort out inputs -----------------------------------------------------------
  month <- as.integer(month)

  if (month %!in% 1L:12L | !is.numeric(month)) {
    stop("Enter a month 1-12.")
  }

  # Return financial quarter --------------------------------------------------
  dplyr::case_when(
    month %in% c(1,  2,  3)    ~ 4,
    month %in% c(4,  5,  6)    ~ 1,
    month %in% c(7,  8,  9)    ~ 2,
    month %in% c(10, 11, 12)   ~ 3)

}
n-fanton/ndr documentation built on Dec. 21, 2021, 11:07 p.m.