This way of rounding is compliant with the rule used by ISO to assign week to a given year.

round_to_given_day_of_week(
  date = today(),
  week_start = get_week_start(),
  week_min = get_week_min(),
  iso = FALSE,
  locale = NULL,
  ...
)

Arguments

date

Date vector

week_start

integer 1 for Monday and 7 for Sunday (default is based on locale)

week_min

integer A which point should should date start being rounded up?

  • 1L always rounds Up to the next given day

  • 3L rounds the last 4 days of the week to the next given day (HalfDown)

  • 4L (ISO) rounds the last 4 days of the week to the next given day and the first 3 days of the week to the previous given day (HalfUp)

  • 7L always rounds Down to the previous given day

iso

logical Whether to use ISO's definition of the week

locale

definition of the week either logical for current locale or character language code

...

Further arguments for next_day_of_week and prev_day_of_week for applying a custom shift

Value

Datevector

Examples

if (FALSE) {
dates <- seq_date("2022-01-03", "2022-01-09")
plot(dates, sapply(dates, function(x) {
  round_day_of_week(x, week_min = 4)
}), type = "l")
lines(dates, sapply(dates, function(x) {
  round_day_of_week(x, week_min = 7)
}), col = "blue")
lines(dates, sapply(dates, function(x) {
  round_day_of_week(x, week_min = 5)
}), col = "red")
}