Learn R Programming

tsbox (version 0.3.1)

ts_lag: Lag or Lead of Time Series

Description

Shift time stamps in ts-boxable time series, either by a number of periods or by a fixed amount of time.

Usage

ts_lag(x, by = 1)

Value

a ts-boxable time series, with the same class as the input. If time stamp shifting causes the object to be irregular, a data frame is returned.

Arguments

x

ts-boxable time series, an object of class ts, xts, zoo, data.frame, data.table, tbl, tbl_ts, tbl_time, tis, irts or timeSeries.

by

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

Details

The lag order, by, is defined the opposite way as in R base. Thus, -1 is a lead and +1 a lag.

If by is integer, the time stamp is shifted by the number of periods. This requires the series to be regular.

If by is character, the time stamp is shifted by a specific amount of time. This can be one of one of "sec", "min", "hour", "day", "week", "month", "quarter" or "year", 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.

Examples

Run this code
# \donttest{
ts_plot(AirPassengers, ts_lag(AirPassengers), title = "The need for glasses")
# }
head(ts_lag(fdeaths, "1 month"))
head(ts_lag(fdeaths, "1 year"))
head(ts_lag(ts_df(fdeaths), "2 day"))
head(ts_lag(ts_df(fdeaths), "2 min"))
head(ts_lag(ts_df(fdeaths), "-1 day"))

Run the code above in your browser using DataLab