Computes an index column from a numeric vector relative to a base element:
idx_type = 100
: classic base-100 index (base value -> 100, others scaled).idx_type = 0
: percent change from base (base value -> 0, others as % difference).
Details
If num_vec
has names, idx_pos
may be the name of the base element (character)
or a numeric (integer-ish) position. If num_vec
is unnamed, idx_pos
must be numeric.
Examples
# Basic usage (base-100)
create_index(c(50, 100, 150))
#> [1] 100 200 300
# Percent change from base (base -> 0%)
create_index(c(50, 100, 150), idx_type = 0)
#> [1] 0 100 200
# Using the second value as base (by position)
create_index(c(90, 100, 120), idx_pos = 2)
#> [1] 90 100 120
# Named vector: select base by name (only works because vector has names)
x <- c(a = 80, b = 100, c = 120)
create_index(x, idx_pos = "b") # base-100 index with "b" as base
#> a b c
#> 80 100 120
create_index(x, idx_pos = "b", idx_type = 0) # percent change from "b"
#> a b c
#> -20 0 20