In this tutorial, we will look at how to get the cumulative minimum in a vector in R with the help of some examples.
What is the cumulative minimum?
The cumulative min in a series of values is the minimum value up to that value in our series. For example, for a vector of three values (a1, a2, and a3), the cumulative minimum would be a1, min(a1,a2), and min(a1,a2,a3). The following image illustrates this with an example.

In the above image, we have five values 6, 3, 4, 1, and 2. The cumulative min for these values is 6, min(6, 3), min(6, 3, 4), min(6, 3, 4, 1), and min(6, 3, 4, 1, 2) respectively. Note that the order in which these values appear is important when computing the cumulative max.
How to calculate the cumulative min of a vector in R?
You can use the cummin()
function in R to compute the cumulative minimum of the values in a vector. Pass the vector as an argument to the function. The following is the syntax –
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
# cumulative min of vector x cummin(x)
It returns a vector containing the cumulative minimum of the values in the passed vector.
Examples
Let’s now look at some examples of using the above syntax.
Cumulative min of a vector of numbers
Let’s create a vector of some numbers and use the cummin()
function to calculate its cumulative minimum. For example, let’s compute the cumulative min for the vector c(1, 3, 2, 6, 4, 5)
.
# create a vector vec <- c(6, 3, 4, 1, 2, 5) # cumulative min of vector print(cummin(vec))
Output:
[1] 6 3 3 1 1 1
We print the resulting vector. You can see that each value in the resulting vector is the minimum of all values till that particular index from the original vector.
Let’s look at another example. What if the values in the vector are in ascending order?
Let’s find out.
# create a vector vec <- c(1, 2, 3, 4, 5, 6) # cumulative min of vector print(cummin(vec))
Output:
[1] 1 1 1 1 1 1
You can see that the cumulative minimum vector contains only 1 as all its values. This is because the first element itself is the minimum value in the entire vector and thus we get the first element, 1 as the cumulative minimum for all the values.
What if the values are in descending order?
Let’s find out.
# create a vector vec <- c(6, 5, 4, 3, 2, 1) # cumulative min of vector print(cummin(vec))
Output:
[1] 6 5 4 3 2 1
We get the same vector as the cumulative min vector. This is because the values in the original vector are in descending order thus each new value is smaller than all the previous values.
Cumulative min of a vector with NA
values
What would happen if you apply the cummin()
function to a vector containing some NA
values?
Let’s find out.
For this, we will create a vector with some NA
values and then apply the cummin()
function.
# create a vector vec <- c(6, 3, 4, NA, 2, NA, 5) # cumulative min of vector print(cummin(vec))
Output:
[1] 6 3 3 NA NA NA NA
You can see that we get the cumulative min till we encounter the first NA
in our vector. From this point onwards, the resulting cumulative min for all the values is NA
. This happens because performing any arithmetic operation with NA
results in an NA
in R.
If you want to compute the cumulative min irrespective of the NA
values, you can first remove the NA
values from the vector and then apply the cummin()
function.
You might also be interested in –
- R – Sum of values in a Vector
- Average of Values in an R Vector
- Get the Maximum value in an R Vector
- Get the Minimum Value in an R Vector
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.