Extract Target Name and Level from Target String
get_target_level.RdParses 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:
Iterate through all target names in
target_map.For each target name, check if
target_strstarts with that prefix plus "_".If a match is found, extract everything after the prefix as the level.
If no match is found, return the original string as target and NA for level.
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)