rguides

cummin()

cummin(x)

The cummin() function computes the cumulative minimum of a vector, returning a vector of the same length where each element is the minimum of all preceding elements.

Syntax

cummin(x)

Parameters

ParameterTypeDefaultDescription
xnumericrequiredA numeric vector

Examples

Basic usage

The cummin() function is the complement of cummax(), scanning left to right and tracking the smallest value encountered at each position. The result is always the same length as the input and is guaranteed to be non-increasing. This property makes it useful for tracking running worsts, identifying new lows, and monitoring declining trends in sequential data.

x <- c(5, 4, 3, 2, 1)

cummin(x)
# [1] 5 4 3 2 1

# With increasing values
y <- c(1, 2, 3, 4, 5)
cummin(y)
# [1] 1 1 1 1 1

# With mixed values
z <- c(3, 1, 4, 1, 5)
cummin(z)
# [1] 3 1 1 1 1

Tracking running minimum

Tracking the running minimum of stock prices identifies the lowest value observed up to each trading day. This information, combined with the running maximum, lets you compute the historical range at any point: the spread between the best and worst prices seen so far. A narrowing running range can signal a period of consolidation within a trading instrument before a breakout or trend reversal.

# Stock prices
prices <- c(100, 95, 105, 98, 110, 105, 115)

# Running minimum (lowest so far)
cummin(prices)
# [1] 100  95  95  95  95  95  95

Common patterns

Drawdown tracking

Drawdown analysis pairs cummax() with cummin() to measure declines from peak to trough. While cummax() tracks the historical high, the running minimum helps identify the worst point within a decline: how far the portfolio has fallen from its most recent peak. The maximum drawdown, expressed as a percentage of the peak value, is a standard risk metric that portfolio managers use to quantify the worst-case loss an investment has experienced over a given time window.

# Portfolio values
portfolio <- c(10000, 9800, 10500, 9700, 10100, 9500, 10200)

# Maximum peak-to-trough decline
peak <- cummax(portfolio)
drawdown <- (portfolio - cummin(portfolio)) / peak
min(drawdown)
# [1] -0.1020408  # about 10% drawdown

Lowest temperature tracking

Any domain with repeated measurements can use cummin() to detect new lows. Temperature monitoring, water level tracking, response time logging, and manufacturing quality checks all produce sequences where a new minimum marks a noteworthy event. The running minimum transforms raw readings into a simple record of whether conditions are steadily worsening or whether a floor has been found and held.

# Daily temperatures
temps <- c(72, 68, 75, 65, 70, 62, 71)

# Coldest day so far
cummin(temps)
# [1] 72 68 68 65 65 62 62

cummin() in practice

cummin() returns the running minimum, each position holds the smallest value seen so far. It is the complement of cummax() and useful in the same contexts: tracking the worst observed performance, finding the lowest price in a window, or checking if a declining series has found a floor.

A practical application in quality control: cummin(measurements) >= threshold checks whether all measurements up to each point have been above a quality threshold. The first FALSE in the logical vector marks the first violation.

Like all cumulative functions in R, cummin() is vectorized and runs in C, it processes large vectors efficiently without loops. It propagates NA values, so remove or handle missing data before calling if you need results past the first gap.

cummin() handles ties by keeping the existing minimum (the first occurrence takes precedence). The result is always non-increasing. For quality control or process monitoring, cummin(x) < threshold identifies runs where the series has ever fallen below a threshold. Combined with rle() (run-length encoding), you can count and characterize these violation periods efficiently.

cummin() combined with cummax() lets you compute a running range: cummax(x) - cummin(x) gives the range of values seen so far at each position. This is useful for detecting whether a time series has been trending within a tightening band, or for computing the spread between historical high and low prices up to each observation date in a daily or intraday financial or sensor time series where you need to track the minimum observed value over time.

cummin() handles NA values the same way as cummax(): it propagates NA forward through the sequence. If you need to compute a running minimum while ignoring missing values, you can replace NA with Inf before calling cummin(), cummin(ifelse(is.na(x), Inf, x)) — which ensures NA positions do not disrupt the running minimum. The result will show Inf at those positions rather than NA, which you can then replace if needed.

See also