Tidy Data Patterns
Split Keys
Columns Hidden in Text
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.
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 = "")
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 colsrawparts ← 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$codetidy ← 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 <- 1values this step3 rows x 3 colstidynorth/A, north/B, south/Apartsregion_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_indexregion ← north
4region_index <- 15region <- c("north", "south")[region_index]6total <- sum(tidy$count[tidy$region == region])values this stepnorthregion1region_indextotal ← 8
5region <- c("north", "south")[region_index]6total <- sum(tidy$count[tidy$region == region])7cat(total, "\n", sep = "")values this step8totalnorthregion3, 5tidy$countcat(total, " ", sep = "")
6total <- sum(tidy$count[tidy$region == region])7cat(total, "\n", sep = "")output8values this step8total
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 colsrawparts ← 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$codetidy ← 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 <- 2values this step3 rows x 3 colstidynorth/A, north/B, south/Apartsregion_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_indexregion ← south
4region_index <- 25region <- c("north", "south")[region_index]6total <- sum(tidy$count[tidy$region == region])values this stepsouthregion2region_indextotal ← 4
5region <- c("north", "south")[region_index]6total <- sum(tidy$count[tidy$region == region])7cat(total, "\n", sep = "")values this step4totalsouthregion4tidy$countcat(total, " ", sep = "")
6total <- sum(tidy$count[tidy$region == region])7cat(total, "\n", sep = "")output4values 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.