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

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:



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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


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.

My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.