var_shift: Shift variables forward or backward by some observations

Description Usage Arguments Value Examples

View source: R/var_shift.R

Description

Sometimes we want to shift some measurements backward or forward. dplyr::lead() and dplyr::lag() do this but will only operate on a single variable. shift() extends this functionality and allows shifting mutiple variables by a range of steps.

Usage

1
var_shift(df, vars, n, fn)

Arguments

df

Data frame containing variables to shift.

vars

Variable(s) to shift.

n

Number of observations to shift.

fn

The shifting function. Usually this is dplyr::lead() or dplyr::lag(). This could also be a custom function that takes a vector and a number n.

Value

Data frame with append shifted measurements.

Examples

1
2
3
4
## Not run: 
shift(mtcars, c("vs", "am"), lead, 1:2)

## End(Not run)

MohoWu/ricardor documentation built on March 6, 2021, 4:17 p.m.