Created
July 5, 2016 16:50
-
-
Save sfirke/08f281190690861779653b3ac13b8621 to your computer and use it in GitHub Desktop.
Function for turning Date into school year string
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
## Date to SY function | |
## Year of 2nd argument does not matter | |
## Turns 2015-10-02 into "2015-16", and 2016-04-05 into "2015-16", with cutoff day = 2010-07-01 | |
date_to_sy <- function(date_var, last_day_of_sy){ | |
if(!(is.Date(date_var) & is.Date(last_day_of_sy))){stop("`date_var` and `last_day_of_sy` must both be class Date")} | |
cutoff_day <- day(last_day_of_sy) | |
cutoff_month <- month(last_day_of_sy) | |
case_when( | |
is.na(date_var) ~ as.character(NA), | |
month(date_var) > cutoff_month ~ paste0(year(date_var), " - ", year(date_var) + 1), # if past cutoff, X - X+! | |
month(date_var) == cutoff_month & day(date_var) > cutoff_day ~ paste0(year(date_var), " - ", year(date_var) + 1), # past cutoff | |
TRUE ~ paste0(year(date_var) - 1, " - ", year(date_var)) # prior to cutoff = X-1 to X | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment