Skip to contents

Parses a target string to extract the target name and the specific level (category/range/code). For example, given "p_age_0_4", returns target = "p_age", level = "0_4". If no prefix match is found, returns the full string as the target and NA for level.

Steps:

  1. Iterate through all target names in target_map.

  2. For each target name, check if target_str starts with that prefix plus "_".

  3. If a match is found, extract everything after the prefix as the level.

  4. If no match is found, return the original string as target and NA for level.

Usage

get_target_level(target_str, target_map)

Arguments

target_str

Character string, e.g., "p_age_0_4".

target_map

Named list as returned by get_target_map(), with targets as names.

Value

List with elements:

target

Character. Target variable name.

level

Character. Level/category string, or NA if no level found.

Examples

target_map <- list(p_age = list(), p_income = list())
get_target_level("p_age_0_4", target_map)
#> $target
#> [1] "p_age"
#> 
#> $level
#> [1] "0_4"
#> 
# Returns: list(target = "p_age", level = "0_4")

get_target_level("h_total", target_map)
#> $target
#> [1] "h_total"
#> 
#> $level
#> [1] NA
#> 
# Returns: list(target = "h_total", level = NA)