Skip to contents

A function to help build random walks by mutating a data frame.

Usage

rand_walk_helper(.data, .value)

Arguments

.data

The data frame to mutate.

.value

The .initial_value to use. This is passed from the random walk function being called by the end user.

Value

A modified data frame/tibble with the following columns added:

  • cum_sum: Cumulative sum of y.

  • cum_prod: Cumulative product of y.

  • cum_min: Cumulative minimum of y.

  • cum_max: Cumulative maximum of y.

  • cum_mean: Cumulative mean of y.

Details

A function to help build random walks by mutating a data frame. This mutation adds the following columns to the data frame: cum_sum, cum_prod, cum_min, cum_max, and cum_mean. The function is used internally by certain functions that generate random walks.

See also

Other Utility Functions: convert_snake_to_title_case(), generate_caption()

Author

Steven P. Sanderson II, MPH

Examples

df <- data.frame(
  walk_number = factor(rep(1L:25L, each = 30L)),
  x = rep(1L:30L, 25L),
  y = rnorm(750L, 0L, 1L)
  )

rand_walk_helper(df, 100)
#> # A tibble: 750 × 8
#>    walk_number     x       y cum_sum cum_prod cum_min cum_max cum_mean
#>    <fct>       <int>   <dbl>   <dbl>    <dbl>   <dbl>   <dbl>    <dbl>
#>  1 1               1  0.551    101.    155.     101.     101.    101. 
#>  2 1               2 -2.09      98.5  -169.      97.9    101.     99.2
#>  3 1               3 -0.0249    98.4  -165.      97.9    101.     99.5
#>  4 1               4  0.888     99.3  -311.      97.9    101.     99.8
#>  5 1               5 -0.337     99.0  -206.      97.9    101.     99.8
#>  6 1               6 -1.03      98.0     6.33    97.9    101.     99.7
#>  7 1               7  0.431     98.4     9.05    97.9    101.     99.8
#>  8 1               8  0.209     98.6    10.9     97.9    101.     99.8
#>  9 1               9 -1.55      97.0    -6.00    97.9    101.     99.7
#> 10 1              10 -1.17      95.9     1.04    97.9    101.     99.6
#> # ℹ 740 more rows