Skip to contents

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).

Usage

create_index(num_vec, idx_pos = 1, idx_type = 100)

Arguments

num_vec

A numeric vector of length > 1.

idx_pos

A base selector: either a 1-based (integer-ish) position, or—only if num_vec has names—a single character string matching one name.

idx_type

Numeric scalar, either 100 (base-100 index) or 0 (percent change). Default 100.

Value

A numeric vector of indexed values (same length as num_vec).

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