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

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(), ts_growth_rate_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.0130 0.535   NA      
#>  2 2021-02-01  0.335  0.596    0.0608 
#>  3 2021-03-01 -1.59   0.619    0.0238 
#>  4 2021-04-01 -0.290  0.367   -0.253  
#>  5 2021-05-01 -0.195  0.444    0.0770 
#>  6 2021-06-01 -0.956  0.965    0.521  
#>  7 2021-07-01 -0.869  0.659   -0.306  
#>  8 2021-08-01  0.833  0.800    0.141  
#>  9 2021-09-01  1.33   0.799   -0.00112
#> 10 2021-10-01 -1.16   0.670   -0.129