In this tutorial, we will look at how to get the cumulative maximum in a vector in R with the help of some examples.
What is the cumulative maximum?
The cumulative max in a series of values is the maximum value up to that value in our series. For example, for a vector of three values (a1, a2, and a3), the cumulative maximum would be a1, max(a1,a2), and max(a1,a2,a3). The following image illustrates this with an example.
In the above image, we have five values 1, 3, 2, 6, and 4. The cumulative max for these values is 1, max(1, 3), max(1, 3, 2), max(1, 3, 2, 6), and max(1, 3, 2, 6, 4) respectively. Note that the order in which these values appear is important when computing the cumulative max.
How to calculate the cumulative max of a vector in R?
You can use the
cummax() function in R to compute the cumulative maximum of the values in a vector. Pass the vector as an argument to the function. The following is the syntax –
# cumulative max of vector x cummax(x)
It returns a vector containing the cumulative maximum of the values in the passed vector.
Let’s now look at some examples of using the above syntax.
Cumulative max of a vector of numbers
Let’s create a vector of some numbers and use the
cummax() function to calculate its cumulative maximum. For example, let’s compute the cumulative max for the vector
c(1, 3, 2, 6, 4, 5).
# create a vector vec <- c(1, 3, 2, 6, 4, 5) # cumulative max of vector print(cummax(vec))
 1 3 3 6 6 6
We print the resulting vector. You can see that each value in the resulting vector is the maximum 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 max of vector print(cummax(vec))
 1 2 3 4 5 6
We get the same vector as the cumulative max. This is because the values in the original vector are in ascending order thus each new value is greater than all the previous 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 max of vector print(cummax(vec))
 6 6 6 6 6 6
You can see that the cumulative maximum contains only 6 as all its values. This is because the first element itself is the maximum value in the entire vector and thus we get the first element, 6 as the cumulative maximum for all the values.
Cumulative max of a vector with
What would happen if you apply the
cummax() function to a vector containing some
Let’s find out.
For this, we will create a vector with some
NA values and then apply the
# create a vector vec <- c(1, 3, 2, NA, 4, NA, 5) # cumulative max of vector print(cummax(vec))
 1 3 3 NA NA NA NA
You can see that we get the cumulative max till we encounter the first
NA in our vector. From this point onwards, the resulting cumulative max 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 max irrespective of the
NA values, you can first remove the
NA values from the vector and then apply the
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.