time_shift adds seconds, minutes, hours, days, weeks, months, quarters or years to dates.

time_shift(x, by = NULL)

Arguments

x

Date or POSIXct. If POSIXct, it is converted into Date.

by

integer or character, either the number of shifting periods (integer), or an absolute amount of time (character). See details.

Value

an object of class Date

Details

If by is character, the time stamp is shifted by a specific amount of time. This can be one of "sec", "min", "hour", "day", "week", "month", "quarter" or"year", or an abbreviation, optionally preceded by a (positive or negative) integer and a space, or followed by plural "s". This is passed to base::seq.Date(). This does not require the series to be regular.

See also

ts_frequency() for standard aggregation.

Examples

ap.time <- ts_df(AirPassengers)$time head(time_shift(ap.time, 14))
#> [1] "1950-03-01" "1950-04-01" "1950-05-01" "1950-06-01" "1950-07-01" #> [6] "1950-08-01"
head(time_shift(ap.time, "7 week"))
#> [1] "1949-02-19" "1949-03-19" "1949-04-19" "1949-05-19" "1949-06-19" #> [6] "1949-07-19"
head(time_shift(ap.time, "-1 month"))
#> [1] "1948-12-01" "1949-01-01" "1949-02-01" "1949-03-01" "1949-04-01" #> [6] "1949-05-01"
time_shift(ts_end(mdeaths), 1)
#> [1] "1979-12-02"
time_shift(ts_end(mdeaths), "-14 sec")
#> [1] "1979-12-01 00:59:46 CET"
time_shift(ts_end(mdeaths), "-1 year")
#> [1] "1978-12-01"
ts_span(ts_c(mdeaths, fdeaths), start = time_shift(ts_end(mdeaths), -1))
#> mdeaths fdeaths #> Dec 1979 1341 574