Find the closest instance of the requested weekday to min(x). Helper function for thicken with the interval "week", when the user desires the start day of the weeks to be different from Sundays.

closest_weekday(x, wday = 1, direction = c("down", "up"))

Arguments

x

A vector of class Date, POSIXct, or POSIXlt.

wday

Integer in the range 0-6 specifying the desired weekday start (0 = Sun, 1 = Mon, 2 = Tue, 3 = Wed, 4 = Thu, 5 = Fri, 6 = Sat).

direction

The first desired weekday before ("down") or after ("up") the first day in x.

Value

Object of class Date, reflecting the closest desired weekday to x.

Examples

closest_weekday(coffee$time_stamp)
#> [1] "2016-07-04"
closest_weekday(coffee$time_stamp, 5)
#> [1] "2016-07-01"
closest_weekday(coffee$time_stamp, 1, direction = "up")
#> [1] "2016-07-11"
closest_weekday(coffee$time_stamp, 5, direction = "up")
#> [1] "2016-07-08"