Rescale variables to start values by group
rel_start.Rd
Convenience wrapper of dplyr::group_by
and dplyr::mutate
to
scale variables, especially time-series, by series start values
Examples
test_df <- data.frame(grp = rep(1:3, each = 10)
, val = rep(10:1, times = 3))
test_df %>%
rel_start(col_vars = val, group_vars = grp) %>%
print(n = 30)
#> # A tibble: 30 × 3
#> # Groups: grp [3]
#> grp val rel_val
#> <int> <int> <dbl>
#> 1 1 10 1
#> 2 1 9 0.9
#> 3 1 8 0.8
#> 4 1 7 0.7
#> 5 1 6 0.6
#> 6 1 5 0.5
#> 7 1 4 0.4
#> 8 1 3 0.3
#> 9 1 2 0.2
#> 10 1 1 0.1
#> 11 2 10 1
#> 12 2 9 0.9
#> 13 2 8 0.8
#> 14 2 7 0.7
#> 15 2 6 0.6
#> 16 2 5 0.5
#> 17 2 4 0.4
#> 18 2 3 0.3
#> 19 2 2 0.2
#> 20 2 1 0.1
#> 21 3 10 1
#> 22 3 9 0.9
#> 23 3 8 0.8
#> 24 3 7 0.7
#> 25 3 6 0.6
#> 26 3 5 0.5
#> 27 3 4 0.4
#> 28 3 3 0.3
#> 29 3 2 0.2
#> 30 3 1 0.1
test_df %>%
rel_start(col_vars = "val", group_vars = "grp") %>%
print(n = 30)
#> # A tibble: 30 × 3
#> # Groups: grp [3]
#> grp val rel_val
#> <int> <int> <dbl>
#> 1 1 10 1
#> 2 1 9 0.9
#> 3 1 8 0.8
#> 4 1 7 0.7
#> 5 1 6 0.6
#> 6 1 5 0.5
#> 7 1 4 0.4
#> 8 1 3 0.3
#> 9 1 2 0.2
#> 10 1 1 0.1
#> 11 2 10 1
#> 12 2 9 0.9
#> 13 2 8 0.8
#> 14 2 7 0.7
#> 15 2 6 0.6
#> 16 2 5 0.5
#> 17 2 4 0.4
#> 18 2 3 0.3
#> 19 2 2 0.2
#> 20 2 1 0.1
#> 21 3 10 1
#> 22 3 9 0.9
#> 23 3 8 0.8
#> 24 3 7 0.7
#> 25 3 6 0.6
#> 26 3 5 0.5
#> 27 3 4 0.4
#> 28 3 3 0.3
#> 29 3 2 0.2
#> 30 3 1 0.1