Open In App

Calculate the Cumulative Maxima of a Vector in R Programming – cummax() Function

Last Updated : 11 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The cumulative maxima is the max value of elements 1 through l for an element l of the given variables.

cummax() function in R Language is used to calculate the cumulative maxima of the values of vector passed as arguments.

Syntax: cummax(x) 

Parameters: 

x: numeric object

Example 1: 

Python3




# R program to cumulative maxima
 
# Calling cummax() function
cummax(6:2)
cummax(-2:-6)
cummax(3.8:1.2)


Output:

[1] 6 6 6 6 6
[1] -2 -2 -2 -2 -2
[1] 3.8 3.8 3.8

Example 2: 

Python3




# R program to cumulative maxima
 
# Creating vectors
x1 <- c(5, 3, 2, 6, 3, 4)
x2 <- c(-4, 6, 3)
 
# Calling cummax() function
cummax(x1)
cummax(x2)


Output:

[1] 5 5 5 6 6 6
[1] -4  6  6

Since, the maxima is calculated for the values in decreasing order and hence the above code shows two cumulative values for vectors.

Example :

the example of how to calculate the cumulative maxima of a vector in R using the cummax() function.

R




# Create a sample vector
v <- c(1, 6, 2, 7, 5, 9)
 
# Calculate the cumulative maxima of the vector
cumulative_max <- cummax(v)
 
# Print the results
print(cumulative_max)


output :

[1] 1 6 6 7 7 9


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads