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

111 views Asked by At

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

2

There are 2 answers

2
talat On BEST ANSWER

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".

4
smci On

"get successive high values (of value?)" is unclear. It seems you want to filter only rows whose value is higher than previous max.

First, we reorder your df in increasing order of value... (not clear but I think that's what you wanted)

Then we use logical indexing with diff()>0 to only include strictly-increasing rows:

rdf <- df[order(df$value),]

rdf[ diff(rdf$value)>0, ]
   order value
1      1     1
9      9     2
10    10     3
4      4     5
2      2     7
7      7     9
8      8    10