R/parse_monthyear.R

#' Parse month and year
#'
#' @param string Input vector. Either a character vector, or something coercible to one.
#'
#' @return an integer in the format 'YYYYMM'. This facilitates specifying conditions on which data is filtered.
#' @export
#'
#' @examples
#' string <- "Mar 2000"
#' parse_monthYear(string)
parse_monthYear <- function(string){
  month = string %>% str_extract('[A-Za-z]+') %>%
    str_replace('Jan', '01') %>%
    str_replace('Feb', '02') %>%
    str_replace('Mar', '03') %>%
    str_replace('Apr', '04') %>%
    str_replace('May', '05') %>%
    str_replace('Jun', '06') %>%
    str_replace('Jul', '07') %>%
    str_replace('Aug', '08') %>%
    str_replace('Sep', '09') %>%
    str_replace('Oct', '10') %>%
    str_replace('Nov', '11') %>%
    str_replace('Dec', '12')

  year = string %>% str_extract('[0-9]{4}')

  output = paste0(year, month) %>% as.integer()

  return(output)
}
Framus94/HierarchiesAndCareers documentation built on June 5, 2019, 8:52 a.m.