Closed cole-brokamp closed 8 months ago
Need a better way to match.
get_closest_year <- function(date, years) { date_year <- as.numeric(format(date, "%Y")) purrr::map_chr(date_year, \(x) as.character(years[which.min(abs(as.numeric(years) - x))])) }
Ties mean order of supplied years changes output:
get_closest_year(as.Date(c("2021-09-15", "2022-09-01")), years = c(2020, 2022)) # [1] "2020" "2022" get_closest_year(as.Date(c("2021-09-15", "2022-09-01")), years = c(2022, 2020)) # [1] "2022" "2022"
Need a better way to match.
Ties mean order of supplied years changes output: