Compact labels often hide multiple variables. Splitting a key turns those variables into separate columns.

Program

Play the script to split region-group codes, choose a region, and total its counts.

region_index
split_keys.R
Replay: real traced execution (multi-file project)
raw <- data.frame(code = c("north_A", "north_B", "south_A"), count = c(3, 5, 4))
parts <- do.call(rbind, strsplit(raw$code, "_", fixed = TRUE))
tidy <- data.frame(region = parts[, 1], group = parts[, 2], count = raw$count)
region_index <- 1
region <- c("north", "south")[region_index]
total <- sum(tidy$count[tidy$region == region])
cat(total, "\n", sep = "")
raw <- data.frame(code = c("north_A", "north_B", "south_A"), count = c(3, 5, 4))
parts <- do.call(rbind, strsplit(raw$code, "_", fixed = TRUE))
tidy <- data.frame(region = parts[, 1], group = parts[, 2], count = raw$count)
region_index <- 2
region <- c("north", "south")[region_index]
total <- sum(tidy$count[tidy$region == region])
cat(total, "\n", sep = "")
  1. raw ← 3 rows x 2 cols

    1raw <- data.frame(code = c("north_A", "north_B", "south_A"), count = c(3, 5, 4))2parts <- do.call(rbind, strsplit(raw$code, "_", fixed = TRUE))
    values this step3 rows x 2 colsraw
  2. parts ← north/A, north/B, south/A

    1raw <- data.frame(code = c("north_A", "north_B", "south_A"), count = c(3, 5, 4))2parts <- do.call(rbind, strsplit(raw$code, "_", fixed = TRUE))3tidy <- data.frame(region = parts[, 1], group = parts[, 2], count = raw$count)
    values this stepnorth/A, north/B, south/Apartsnorth_A, north_B, south_Araw$code
  3. tidy ← 3 rows x 3 cols

    2parts <- do.call(rbind, strsplit(raw$code, "_", fixed = TRUE))3tidy <- data.frame(region = parts[, 1], group = parts[, 2], count = raw$count)4region_index <- 1
    values this step3 rows x 3 colstidynorth/A, north/B, south/Aparts
  4. region_index ← 1

    3tidy <- data.frame(region = parts[, 1], group = parts[, 2], count = raw$count)4region_index <- 15region <- c("north", "south")[region_index]
    values this step1region_index
  5. region ← north

    4region_index <- 15region <- c("north", "south")[region_index]6total <- sum(tidy$count[tidy$region == region])
    values this stepnorthregion1region_index
  6. total ← 8

    5region <- c("north", "south")[region_index]6total <- sum(tidy$count[tidy$region == region])7cat(total, "\n", sep = "")
    values this step8totalnorthregion3, 5tidy$count
  7. cat(total, " ", sep = "")

    6total <- sum(tidy$count[tidy$region == region])7cat(total, "\n", sep = "")
    output8
    values this step8total
  1. raw ← 3 rows x 2 cols

    1raw <- data.frame(code = c("north_A", "north_B", "south_A"), count = c(3, 5, 4))2parts <- do.call(rbind, strsplit(raw$code, "_", fixed = TRUE))
    values this step3 rows x 2 colsraw
  2. parts ← north/A, north/B, south/A

    1raw <- data.frame(code = c("north_A", "north_B", "south_A"), count = c(3, 5, 4))2parts <- do.call(rbind, strsplit(raw$code, "_", fixed = TRUE))3tidy <- data.frame(region = parts[, 1], group = parts[, 2], count = raw$count)
    values this stepnorth/A, north/B, south/Apartsnorth_A, north_B, south_Araw$code
  3. tidy ← 3 rows x 3 cols

    2parts <- do.call(rbind, strsplit(raw$code, "_", fixed = TRUE))3tidy <- data.frame(region = parts[, 1], group = parts[, 2], count = raw$count)4region_index <- 2
    values this step3 rows x 3 colstidynorth/A, north/B, south/Aparts
  4. region_index ← 2

    3tidy <- data.frame(region = parts[, 1], group = parts[, 2], count = raw$count)4region_index <- 25region <- c("north", "south")[region_index]
    values this step2region_index
  5. region ← south

    4region_index <- 25region <- c("north", "south")[region_index]6total <- sum(tidy$count[tidy$region == region])
    values this stepsouthregion2region_index
  6. total ← 4

    5region <- c("north", "south")[region_index]6total <- sum(tidy$count[tidy$region == region])7cat(total, "\n", sep = "")
    values this step4totalsouthregion4tidy$count
  7. cat(total, " ", sep = "")

    6total <- sum(tidy$count[tidy$region == region])7cat(total, "\n", sep = "")
    output4
    values this step4total
encoded key `code` combines two variables in one text field.
strsplit `strsplit(..., fixed = TRUE)` splits each code at the underscore.
separate columns `region` and `group` become explicit columns for filtering and summaries.