stlag: Lag/Forward operator in time series

View source: R/lag.R

stlagR Documentation

Lag/Forward operator in time series

Description

Calculate the lag/forward terms of variables and vectors

Usage

stlag(x, ...)

## Default S3 method:
stlag(x, time, n = 1L)

## S3 method for class 'data.frame'
stlag(df, varlist = NULL, time = NULL, by = NULL, n = 1L, mode = NULL)

Arguments

x

a vector.

time

a interger vector object for x, and a name or string for df. For x, time length must equal to x.

n

an integer.

df

a a data.table or data.frame object. If hoping update by reference, data.table is required.

varlist

Variable names needed to calculate lagged terms. a bare name and a character vector are acceptable.

by

Names of variables used to grouping data.

mode

NULL or character string nameing an atomic mode or "list". Default is NULL, update data.frame by reference using ⁠:=⁠ from data.table. list, data.table, data.frame and vector is acceptable. If mode = "vector", the length of varlist needed to be one.

Methods (by class)

  • stlag(default): Calculate lag term of vector

  • stlag(data.frame): Calculate lag terms of data.frame/data.table variables

Examples

time = rep(2001:2006)
x = sample(6)
stlag(x, time)

ts <- data.table(time = 2001:2009, x = 1:9, y = sample(letters, 9))
stlag(ts, x, time = "time", mode = "list")
stlag(ts, x, time)
stlag(ts, y, time, n = -1L)
xt <- data.frame(
    id = rep(c("a", "b"), each = 5),
    time = rep(2001:2005, 2),
    x = 1:10,
    y = sample(letters, 10, replace = TRUE),
    z = sample(LETTERS, 10, replace = TRUE)
)
stxtset(xt, id, time)
stlag(xt, x, n = -1L, mode = "list")
stlag(setDT(xt), x)
xt

liubianshi/lbs documentation built on Nov. 2, 2023, 11:06 a.m.