Build running totals with cumsum
cumsum(a, b, c, ...) returns cumulative sums. Instead of producing one total, it records the running total after each supplied value. For inputs 2, 3, and 5, the result is 2, 5, 10.
When running totals help
Cumulative sums are ideal for spending balances, accumulated distance, frequency distributions, and time-series progress. Compare sum, which returns only the final total. A cumulative sequence can be paired with diff to recover successive changes, subject to the usual initial-value convention.
Examples
Use cumsum(4, -1, 6) to see a balance move from 4 to 3 to 9. With a vector or matrix, the function follows its supported collection semantics, so inspect the result shape with size. If source values require preparation, use map for a transformation or filter to retain qualifying entries.
Input and precision caveats
Each input should be numeric or compatible with addition. A running total preserves every intermediate rounding effect, so long floating-point sequences can differ slightly from an exact symbolic total. Keep units consistent—adding dollars to percentages is meaningless—and validate missing data before accumulation. For an average that evolves with observations, combine the cumulative total with count rather than dividing by a fixed number.