make_shift: Build a List of Expressions (Lags and Leads)

Description Usage Arguments Details Value Examples

Description

This function builds an expression to make multiple lags and/or leads. NOTE: For use inside dplyr::mutate() only!

Usage

1
make_shift(..., .n = 1)

Arguments

...

variables

.n

integer vector with steps to shift:

- positive for lag()

- negative for lead()

- 0 automaticly excluded (so you may use colon notation -2:2)

Details

Unquotation is needed inside the mutate() with !!! (bang-bang-bang) like this:

mutate(mtcars, !!!make_shift(am, mpg))

Value

expression to unquote inside mutate() function

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
make_shift(am, mpg)
#> $am_lag1
#> dplyr::lag(am, 1)
#> 
#> $mpg_lag1
#> dplyr::lag(mpg, 1)

mutate(mtcars[c("am", "mpg")], !!!make_shift(am, mpg, .n = c(-1, 1, 2)))
#>    am  mpg am_lead1 am_lag1 am_lag2 mpg_lead1 mpg_lag1 mpg_lag2
#> 1   1 21.0        1      NA      NA      21.0       NA       NA
#> 2   1 21.0        1       1      NA      22.8     21.0       NA
#> 3   1 22.8        0       1       1      21.4     21.0     21.0
#> 4   0 21.4        0       1       1      18.7     22.8     21.0
#> 5   0 18.7        0       0       1      18.1     21.4     22.8
#> 6   0 18.1        0       0       0      14.3     18.7     21.4

pavel-filatov/featr documentation built on May 12, 2019, 1:29 a.m.