R/month_names.R

Defines functions month_name month_number

Documented in month_name month_number

#' Find the number of a month from name
#'
#' @param month_name Name of a month
#'
#' @return A number, 1-12
#' @export
#'
#' @examples month_number("January")
month_number <- function(month_name) {

  # Sort out inputs -----------------------------------------------------------
  month_name <- stringr::str_to_lower(month_name)

  # Return the month requested ------------------------------------------------
  dplyr::case_when(month_name == "january"   ~  1,
                   month_name == "february"  ~  2,
                   month_name == "march"     ~  3,
                   month_name == "april"     ~  4,
                   month_name == "may"       ~  5,
                   month_name == "june"      ~  6,
                   month_name == "july"      ~  7,
                   month_name == "august"    ~  8,
                   month_name == "september" ~  9,
                   month_name == "october"   ~ 10,
                   month_name == "november"  ~ 11,
                   month_name == "december"  ~ 12)
}

#' Find the name of a month from number
#'
#' @param month_number Number, 1-12
#'
#' @return Name of month
#' @export
#'
#' @examples month_name(4)
month_name <- function(month_number) {

  # Sort out inputs -----------------------------------------------------------
  month_number <- as.numeric(month_number)

  if (month_number %!in% 1L:12L) {
    stop("Please enter a month number from 1 to 12.")
  }

  # Return month name from base data ------------------------------------------
  base::month.name[month_number]
}
n-fanton/ndr documentation built on Dec. 21, 2021, 11:07 p.m.