var_shift: Shift variables forward or backward by some observations

View source: R/var_shift.R

var_shiftR Documentation

Shift variables forward or backward by some observations

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. var_shift() extends this functionality and allows shifting multiple variables by a range of steps.

Usage

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

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

## End(Not run)

MohoWu/ricardor documentation built on March 24, 2022, 4:39 p.m.