Description Usage Arguments Details Value Examples
This function builds an expression to make multiple lags and/or leads.
NOTE: For use inside dplyr::mutate()
only!
1 | make_shift(..., .n = 1)
|
... |
variables |
.n |
integer vector with steps to shift: - positive for - negative for - 0 automaticly excluded (so you may use colon notation |
Unquotation is needed inside the mutate()
with !!!
(bang-bang-bang) like this:
mutate(mtcars, !!!make_shift(am, mpg))
expression to unquote inside mutate()
function
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
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.