Search code examples
rdataframerowfilter

Efficient method of obtaining successive high values of data.frame column


Lets say I have the following data.frame in R

df <- data.frame(order=(1:10),value=c(1,7,3,5,9,2,9,10,2,3))

Other than looping through data an testing whether value exceeds previous high value how can I get successive high values so that I can end up with a table like this

order   value
 1        1
 2        7
 5        9
 8       10

TIA


Solution

  • Here's one option, if I understood the question correct:

    df[df$value > cummax(c(-Inf, head(df$value, -1))),]
    #  order value
    #1     1     1
    #2     2     7
    #5     5     9
    #8     8    10
    

    I use cummax to keep track of the maximum of column "value" and compare it (the previous row's cummax) to each "value" entry. To make sure the first entry is also selected, I start by "-Inf".