Skip to contents

Find the index of a compound range (e.g., "0_4", "2_plus") in a numeric levels vector. Use to map string categories to numeric indices for tabulation and reporting.

Usage

find_level_idx(level_str, levels, label_levels)

Arguments

level_str

character(1). Category or range string (e.g., "0_4", "2_plus").

levels

numeric vector. Sorted levels.

label_levels

character vector. Labels for each level.

Value

integer. Index in levels vector or NA if not found.

Details

  • Returns last index for "_plus" categories.

  • For ranges, returns index of highest numeric value in string.

  • Falls back to direct match or NA if not found.

  • Assumes sorted levels and valid labels.

Settings

None.

See also

  • get_target_map

  • scripts/reporting/find_level_idx.R

Other reporting utilities: get_target_map(), summarize_data(), summarize_pums(), summarize_survey(), tabulate_target()

Examples

find_level_idx("0_4", c(0, 4, 8), c("0-4","5-8","9+"))
#> [1] 2
find_level_idx("2_plus", c(0, 1, 2), c("0","1","2+"))
#> [1] 3