Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save x-raizor/bd3e96e982923abbed4ff0307a9149ab to your computer and use it in GitHub Desktop.
Save x-raizor/bd3e96e982923abbed4ff0307a9149ab to your computer and use it in GitHub Desktop.
Vectorized version
first_date_by_cmonth <- function(cmonth, base_year = 2017) {
require(magrittr)
require(dplyr)
require(purrr)
data.frame(
index = seq(1, length(cmonth)),
year = floor((cmonth - 1) / 12) + base_year,
month = cmonth %% 12,
day = rep(1, length(cmonth))
) %>%
mutate(month = case_when(month == 0 ~ 12, TRUE ~ month)) %>%
split(.$index) %>%
map_chr(function(df) {
paste(df$year, df$month, df$day, sep = '/')
}) %>%
unname() %>%
as.Date()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment