Seasonal summaries group neighboring observations into fixed-size periods.

Program

Play the script to change the group size and see the final period total.

group_size
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 = "")
  1. group_size ← 3

    1group_size <- 32month <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun")
    values this step3group_size
  2. month ← 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, Junmonth
  3. value ← 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, 20value
  4. group ← 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_size
  5. totals ← 31, 52

    4group <- ceiling(seq_along(value) / group_size)5totals <- tapply(value, group, sum)6last_total <- totals[[length(totals)]]
    values this step31, 52totals6 valuesvaluetwo periodsgroup
  6. last_total ← 52

    5totals <- tapply(value, group, sum)6last_total <- totals[[length(totals)]]7label <- paste("last", last_total, sep = ":")
    values this step52last_total31, 52totals
  7. label ← last:52

    6last_total <- totals[[length(totals)]]7label <- paste("last", last_total, sep = ":")8cat(label, "\n", sep = "")
    values this steplast:52label52last_total
  8. cat(label, " ", sep = "")

    7label <- paste("last", last_total, sep = ":")8cat(label, "\n", sep = "")
    outputlast:52
    values this steplast:52label
  1. group_size ← 2

    1group_size <- 22month <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun")
    values this step2group_size
  2. month ← 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, Junmonth
  3. value ← 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, 20value
  4. group ← 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_size
  5. totals ← 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 periodsgroup
  6. last_total ← 37

    5totals <- tapply(value, group, sum)6last_total <- totals[[length(totals)]]7label <- paste("last", last_total, sep = ":")
    values this step37last_total18, 28, 37totals
  7. label ← last:37

    6last_total <- totals[[length(totals)]]7label <- paste("last", last_total, sep = ":")8cat(label, "\n", sep = "")
    values this steplast:37label37last_total
  8. cat(label, " ", sep = "")

    7label <- paste("last", last_total, sep = ":")8cat(label, "\n", sep = "")
    outputlast:37
    values this steplast:37label
  1. group_size ← 6

    1group_size <- 62month <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun")
    values this step6group_size
  2. month ← 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, Junmonth
  3. value ← 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, 20value
  4. group ← 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_size
  5. totals ← 83

    4group <- ceiling(seq_along(value) / group_size)5totals <- tapply(value, group, sum)6last_total <- totals[[length(totals)]]
    values this step83totals6 valuesvalueone periodgroup
  6. last_total ← 83

    5totals <- tapply(value, group, sum)6last_total <- totals[[length(totals)]]7label <- paste("last", last_total, sep = ":")
    values this step83last_total83totals
  7. label ← last:83

    6last_total <- totals[[length(totals)]]7label <- paste("last", last_total, sep = ":")8cat(label, "\n", sep = "")
    values this steplast:83label83last_total
  8. cat(label, " ", sep = "")

    7label <- paste("last", last_total, sep = ":")8cat(label, "\n", sep = "")
    outputlast:83
    values 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.