Time Series Summaries
Seasonal Total
Group Ordered Months
Seasonal summaries group neighboring observations into fixed-size periods.
Program
Play the script to change the group size and see the final period total.
seasonal_total.R
Replay: real traced execution (multi-file project)
group_size <- 3
month <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun")
value <- c(8, 10, 13, 15, 17, 20)
group <- ceiling(seq_along(value) / group_size)
totals <- tapply(value, group, sum)
last_total <- totals[[length(totals)]]
label <- paste("last", last_total, sep = ":")
cat(label, "\n", sep = "")
group_size <- 2
month <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun")
value <- c(8, 10, 13, 15, 17, 20)
group <- ceiling(seq_along(value) / group_size)
totals <- tapply(value, group, sum)
last_total <- totals[[length(totals)]]
label <- paste("last", last_total, sep = ":")
cat(label, "\n", sep = "")
group_size <- 6
month <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun")
value <- c(8, 10, 13, 15, 17, 20)
group <- ceiling(seq_along(value) / group_size)
totals <- tapply(value, group, sum)
last_total <- totals[[length(totals)]]
label <- paste("last", last_total, sep = ":")
cat(label, "\n", sep = "")
group_size ← 3
1group_size <- 32month <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun")values this step3group_sizemonth ← Jan, Feb, Mar, Apr, May, Jun
1group_size <- 32month <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun")3value <- c(8, 10, 13, 15, 17, 20)values this stepJan, Feb, Mar, Apr, May, Junmonthvalue ← 8, 10, 13, 15, 17, 20
2month <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun")3value <- c(8, 10, 13, 15, 17, 20)4group <- ceiling(seq_along(value) / group_size)values this step8, 10, 13, 15, 17, 20valuegroup ← 1, 1, 1, 2, 2, 2
3value <- c(8, 10, 13, 15, 17, 20)4group <- ceiling(seq_along(value) / group_size)5totals <- tapply(value, group, sum)values this step1, 1, 1, 2, 2, 2group3group_sizetotals ← 31, 52
4group <- ceiling(seq_along(value) / group_size)5totals <- tapply(value, group, sum)6last_total <- totals[[length(totals)]]values this step31, 52totals6 valuesvaluetwo periodsgrouplast_total ← 52
5totals <- tapply(value, group, sum)6last_total <- totals[[length(totals)]]7label <- paste("last", last_total, sep = ":")values this step52last_total31, 52totalslabel ← last:52
6last_total <- totals[[length(totals)]]7label <- paste("last", last_total, sep = ":")8cat(label, "\n", sep = "")values this steplast:52label52last_totalcat(label, " ", sep = "")
7label <- paste("last", last_total, sep = ":")8cat(label, "\n", sep = "")outputlast:52values this steplast:52label
group_size ← 2
1group_size <- 22month <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun")values this step2group_sizemonth ← Jan, Feb, Mar, Apr, May, Jun
1group_size <- 22month <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun")3value <- c(8, 10, 13, 15, 17, 20)values this stepJan, Feb, Mar, Apr, May, Junmonthvalue ← 8, 10, 13, 15, 17, 20
2month <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun")3value <- c(8, 10, 13, 15, 17, 20)4group <- ceiling(seq_along(value) / group_size)values this step8, 10, 13, 15, 17, 20valuegroup ← 1, 1, 2, 2, 3, 3
3value <- c(8, 10, 13, 15, 17, 20)4group <- ceiling(seq_along(value) / group_size)5totals <- tapply(value, group, sum)values this step1, 1, 2, 2, 3, 3group2group_sizetotals ← 18, 28, 37
4group <- ceiling(seq_along(value) / group_size)5totals <- tapply(value, group, sum)6last_total <- totals[[length(totals)]]values this step18, 28, 37totals6 valuesvaluethree periodsgrouplast_total ← 37
5totals <- tapply(value, group, sum)6last_total <- totals[[length(totals)]]7label <- paste("last", last_total, sep = ":")values this step37last_total18, 28, 37totalslabel ← last:37
6last_total <- totals[[length(totals)]]7label <- paste("last", last_total, sep = ":")8cat(label, "\n", sep = "")values this steplast:37label37last_totalcat(label, " ", sep = "")
7label <- paste("last", last_total, sep = ":")8cat(label, "\n", sep = "")outputlast:37values this steplast:37label
group_size ← 6
1group_size <- 62month <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun")values this step6group_sizemonth ← Jan, Feb, Mar, Apr, May, Jun
1group_size <- 62month <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun")3value <- c(8, 10, 13, 15, 17, 20)values this stepJan, Feb, Mar, Apr, May, Junmonthvalue ← 8, 10, 13, 15, 17, 20
2month <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun")3value <- c(8, 10, 13, 15, 17, 20)4group <- ceiling(seq_along(value) / group_size)values this step8, 10, 13, 15, 17, 20valuegroup ← 1, 1, 1, 1, 1, 1
3value <- c(8, 10, 13, 15, 17, 20)4group <- ceiling(seq_along(value) / group_size)5totals <- tapply(value, group, sum)values this step1, 1, 1, 1, 1, 1group6group_sizetotals ← 83
4group <- ceiling(seq_along(value) / group_size)5totals <- tapply(value, group, sum)6last_total <- totals[[length(totals)]]values this step83totals6 valuesvalueone periodgrouplast_total ← 83
5totals <- tapply(value, group, sum)6last_total <- totals[[length(totals)]]7label <- paste("last", last_total, sep = ":")values this step83last_total83totalslabel ← last:83
6last_total <- totals[[length(totals)]]7label <- paste("last", last_total, sep = ":")8cat(label, "\n", sep = "")values this steplast:83label83last_totalcat(label, " ", sep = "")
7label <- paste("last", last_total, sep = ":")8cat(label, "\n", sep = "")outputlast:83values this steplast:83label
period groups
`ceiling(seq_along(value) / group_size)` assigns each row to a period.
aggregation
`tapply(value, group, sum)` totals each period.
latest period
The last total summarizes the newest group.