This function takes a column and builds the differences based
on the difference k
between the column and the lag n
specified.
diff_vars(
df,
col = NULL,
lags = 1,
diff = 1,
mutate_type = c("mutate", "trans")
)
dataframe or tibble
string - specify the column/field/variable for differencing.
number - specify the number of lags for differencing.
number - specify the number of differences for differencing.
string - select whether to mutate()
or transmute()
from dplyr.
A dataframe with the lagged differences of the column specified.
Build the differences of a variable
library(magrittr)
df <- datasets::airmiles %>% as.vector() %>% tibble::enframe(name = "airmiles")
diff_vars(df, col = "airmiles") %>% head()
#> Warning: `funs_()` is deprecated as of dplyr 0.7.0.
#> Please use `funs()` instead.
#> See vignette('programming') for more help
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_warnings()` to see where this warning was generated.
#> Warning: `funs()` is deprecated as of dplyr 0.8.0.
#> Please use a list of either functions or lambdas:
#>
#> # Simple named list:
#> list(mean = mean, median = median)
#>
#> # Auto named with `tibble::lst()`:
#> tibble::lst(mean, median)
#>
#> # Using lambdas
#> list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_warnings()` to see where this warning was generated.
#> # A tibble: 6 x 3
#> airmiles value airmiles_diff_1_lag_1
#> <int> <dbl> <int>
#> 1 1 412 NA
#> 2 2 480 1
#> 3 3 683 1
#> 4 4 1052 1
#> 5 5 1385 1
#> 6 6 1418 1