cummax()
cummax(x) Returns:
numeric · Updated March 13, 2026 · Base Functions cumulative vectors cummax base
The cummax() function computes the cumulative maximum of a vector, returning a vector of the same length where each element is the maximum of all preceding elements.
Syntax
cummax(x)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
x | numeric | required | A numeric vector |
Examples
Basic usage
x <- c(1, 2, 3, 4, 5)
cummax(x)
# [1] 1 2 3 4 5
# With decreasing values
y <- c(5, 4, 3, 2, 1)
cummax(y)
# [1] 5 5 5 5 5
# With mixed values
z <- c(3, 1, 4, 1, 5)
cummax(z)
# [1] 3 3 4 4 5
Tracking running maximum
# Sales data
sales <- c(100, 150, 80, 200, 175)
# Running maximum revenue
cummax(sales)
# [1] 100 150 150 200 200
Common Patterns
Peak detection
# Stock prices
prices <- c(100, 95, 105, 98, 110, 105, 115)
# All-time highs
cummax(prices)
# [1] 100 100 105 105 110 110 115
Performance tracking
# Test scores over time
scores <- c(85, 78, 92, 88, 95, 90, 98)
# Best score so far
cummax(scores)
# [1] 85 85 92 92 95 95 98