Learn R Programming

clock (version 0.7.1)

date-and-date-time-shifting: Shifting: date and date-time

Description

date_shift() shifts x to the target weekday. You can shift to the next or previous weekday. If x is currently on the target weekday, you can choose to leave it alone or advance it to the next instance of the target.

There are separate help pages for shifting dates and date-times:

  • dates (Date)

  • date-times (POSIXct/POSIXlt)

Usage

date_shift(x, target, ..., which = "next", boundary = "keep")

Value

x shifted to the target weekday.

Arguments

x

[Date / POSIXct / POSIXlt]

A date or date-time vector.

target

[weekday]

A weekday created from weekday() to target.

Generally this is length 1, but can also be the same length as x.

...

These dots are for future extensions and must be empty.

which

[character(1)]

One of:

  • "next": Shift to the next instance of the target weekday.

  • "previous: Shift to the previous instance of the target weekday.

boundary

[character(1)]

One of:

  • "keep": If x is currently on the target weekday, return it.

  • "advance": If x is currently on the target weekday, advance it anyways.

Examples

Run this code
# See the type specific documentation for more examples

x <- as.Date("2019-01-01") + 0:1

# A Tuesday and Wednesday
as_weekday(x)

monday <- weekday(clock_weekdays$monday)

# Shift to the next Monday
date_shift(x, monday)

Run the code above in your browser using DataLab