View source: R/find_closest_date.R
find_closest_date | R Documentation |
This function finds the closest date from a target date to a date column. It checks if any target dates fall outside the range of the date column (either before the first date or after the last date) and provides warnings for such cases, printing the target dates in different colors based on their relation to the range of the date column.
find_closest_date(target_dates, date_column)
target_dates |
A vector of target dates to find the closest dates for. The dates should be in a format that can be coerced into |
date_column |
A vector of dates representing the date column to compare against. The dates should be in a format that can be coerced into |
The function will warn and print all target dates that are before the first date in the date column in orange, with a message in green and bold/underlined.
It will warn and print all target dates that are after the last date in the date column in navy blue, with a message in dark red, bold, and underlined.
The closest date is selected based on the minimum difference between the target date and the available dates.
If a target date falls outside the range of the date column, the closest date is assigned as either the first or last date in the column accordingly.
The function uses cli
package functionality for colorful text and styled warnings.
A vector of dates from the date column that are closest to each of the target dates.
## Not run:
target_dates <- as.Date(c('2020-01-01', '2023-05-01', '2024-07-01'))
date_column <- as.Date(c('2019-01-01', '2020-06-01', '2021-01-01', '2022-01-01'))
find_closest_date(target_dates, date_column)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.