geomarker-io / appc

Daily, high resolution, near real-time, model-based ambient air pollution exposure assessments in R
http://geomarker.io/appc/
Other
0 stars 0 forks source link

problem with get_closest_year() #14

Closed cole-brokamp closed 8 months ago

cole-brokamp commented 10 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"