What is the cummax() function in R?

Which

Overview

The cummax() function in R obtains the vector's maximum for each index in the vector starting from the beginning of the vector up to the current index.

Syntax

Here is the syntax of the cummax() method:

cunmax(x)
The syntax for the cummax() function

Parameter value

The cummax() function takes a single parameter value, x, which represents a numerical or integer vector.

Example

This is the code for the cummax()method:

# implementing the cummax() function
cummax(1:10)
cummax(c(1, 3, 4, 5, 3, 2, 5, 1, 7, 8, 8, 6))
cummax(c(3, 2, 4, 6, 2, 6, 7, 1, 6, 8, 1, 2))
cummax(c(20, 10, 5, 30, 50, 60, 15, 15))

Explanation

  • Line 2: We implement the cummax() to return a vector object where its maximum elements should start with 1. This is because 1 is the element in the vector object's first index.
  • Line 3: We implement the cummax() function to return a vector object where its maximum elements should start with 1. This is because 1 is the element in the vector object's first index.
  • Line 4: We implement the cummax() function to return a vector object where its maximum elements should start with 3 . This is because 3 is the element in the vector object's first index.
  • Line 5: We implement the cummax() function to return a vector object where its maximum elements should start with 20. This is because 20 is the element in the vector object's first index.

    New on Educative
    Learn to Code
    Learn any Language as a beginner
    Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
    🏆 Leaderboard
    Daily Coding Challenge
    Solve a new coding challenge every day and climb the leaderboard

    Free Resources