Created
August 30, 2019 18:41
-
-
Save x-raizor/bd3e96e982923abbed4ff0307a9149ab to your computer and use it in GitHub Desktop.
Vectorized version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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