time_lag {timeplyr}R Documentation

Time-lagged values

Description

Time-lagged values

Usage

time_lag(
  x,
  k = 1L,
  time = seq_along(x),
  g = NULL,
  time_type = getOption("timeplyr.time_type", "auto"),
  roll_month = getOption("timeplyr.roll_month", "preday"),
  roll_dst = getOption("timeplyr.roll_dst", "boundary")
)

Arguments

x

Vector.

k

Lag size, must be one of the following:

  • string, e.g "day" or "2 weeks"

  • lubridate duration or period object, e.g. days(1) or ddays(1).

  • named list of length one, e.g. list("days" = 7).

  • Numeric vector, e.g. 7.

time

(Optional) time index.
Can be a Date, POSIXt, numeric, integer, yearmon, or yearqtr vector.

g

Grouping object passed directly to collapse::GRP(). This can for example be a vector or data frame.

time_type

If "auto", periods are used for the time expansion when lubridate periods are specified or when days, weeks, months or years are specified, and durations are used otherwise.

roll_month

Control how impossible dates are handled when month or year arithmetic is involved. Options are "preday", "boundary", "postday", "full" and "NA". See ?timechange::time_add for more details.

roll_dst

See ?timechange::time_add for the full list of details.

Value

A vector of length(x) lagged by a specified time unit.

Examples

library(timeplyr)

x <- 1:10
t <- time_seq(Sys.Date(), len = 10, time_by = "3 days")

dplyr::lag(x)
time_lag(x)
time_lag(x, time = t, k = "3 days")

# No values exist at t-1 days
time_lag(x, time = t, k = 1)


[Package timeplyr version 0.5.0 Index]