Skip to contents

Shifts a vector of dates to start on the same day-of-year in a specified origin year while preserving the relative difference in days among the observations. This way the temporal pattern (e.g., seasonality) inherent to the original dates will also be preserved in the shifted dates.

Usage

shift_dates(x, origin = "1970-01-01")

Arguments

x

A vector of date strings or Date objects representing the sequence to shift.

origin

A date string or Date object specifying the desired origin year for the shifted dates. Default is "1970-01-01".

Value

A vector of Date objects with the shifted dates starting on the same day-of-year in the specified origin year.

Details

The primary goal of this function is to align a sequence of dates based on the day-of-year in a desired origin year. This can be particularly useful for comparing or visualizing two or more time series with different absolute dates but aiming to align them based on the day-of-year or another relative metric.

Examples


x <- c("2011-09-14", "2011-09-30", "2011-10-16", "2011-11-01")

shift_dates(x)
#> [1] "1970-09-14" "1970-09-30" "1970-10-16" "1970-11-01"