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 -2.54   0.961      NA    
#>  2 2021-02-01  0.797  0.392      -0.570
#>  3 2021-03-01 -0.587  0.497       0.106
#>  4 2021-04-01 -1.06   0.626       0.129
#>  5 2021-05-01 -0.0775 0.0835     -0.543
#>  6 2021-06-01 -1.25   0.339       0.255
#>  7 2021-07-01 -1.46   0.794       0.456
#>  8 2021-08-01  0.943  0.116      -0.678
#>  9 2021-09-01  1.09   0.803       0.687
#> 10 2021-10-01 -0.571  0.666      -0.138