Skip to contents

Takes a numeric vector and will return the velocity of that vector.

Usage

ts_velocity_augment(.data, .value, .names = "auto")

Arguments

.data

The data being passed that will be augmented by the function.

.value

This is passed rlang::enquo() to capture the vectors you want to augment.

.names

The default is "auto"

Value

A augmented tibble

Details

Takes a numeric vector and will return the velocity of that vector. The velocity of a time series is computed by taking the first difference, so $$x_t - x_t1$$

This function is intended to be used on its own in order to add columns to a tibble.

See also

Other Augment Function: ts_acceleration_augment()

Author

Steven P. Sanderson II, MPH

Examples

suppressPackageStartupMessages(library(dplyr))

len_out    = 10
by_unit    = "month"
start_date = as.Date("2021-01-01")

data_tbl <- tibble(
  date_col = seq.Date(from = start_date, length.out = len_out, by = by_unit),
  a    = rnorm(len_out),
  b    = runif(len_out)
)

ts_velocity_augment(data_tbl, b)
#> # A tibble: 10 × 4
#>    date_col        a      b velocity_b
#>    <date>      <dbl>  <dbl>      <dbl>
#>  1 2021-01-01 -0.486 0.959      NA    
#>  2 2021-02-01 -0.587 0.193      -0.765
#>  3 2021-03-01  0.479 0.777       0.583
#>  4 2021-04-01 -1.35  0.980       0.203
#>  5 2021-05-01 -0.134 0.494      -0.485
#>  6 2021-06-01  0.140 0.206      -0.288
#>  7 2021-07-01 -1.11  0.661       0.455
#>  8 2021-08-01 -0.124 0.0486     -0.612
#>  9 2021-09-01  0.716 0.495       0.446
#> 10 2021-10-01 -1.48  0.265      -0.230