Learn R Programming

statip (version 0.2.3)

lagk: Lag a vector

Description

This function computes a lagged vector, shifting it back or forward.

Usage

lagk(x, k, na = FALSE, cst = FALSE)

Arguments

x

A vector.

k

integer. The number of lags. If k < 0, la serie est avancee au lieu d'etre retardee.

na

logical. If na = TRUE and k > 0 (resp. k < 0), the |k| holes created in the lagged vector are put to NA; otherwise, the imputation depends on cst.

cst

logical. If na = FALSE and cst = TRUE, the |k| holes created in the lagged vector are put to x[[1L]] (or to x[[length(x)]] if k < 0). If na = FALSE and cst = FALSE, these |k| holes are imputed by the k first values of x (or the k last values if k < 0).

Value

A vector of the same type and length as x.

Examples

Run this code
# NOT RUN {
v <- sample(1:10)
print(v)
lagk(v, 1)
lagk(v, 1, na = TRUE)
lagk(v, -2)
lagk(v, -3, na = TRUE)
lagk(v, -3, na = FALSE, cst = TRUE)
lagk(v, -3, na = FALSE)

# }

Run the code above in your browser using DataLab