I have data with a grouping variable 'grps' and a value 'x'. I have calculated the `cummax` within each group 'cmx'. Now I need to find the *second* highest cumulative value of 'x' within each group, `scmx`.

Some data, including the desired column `scmx`:

```
library(data.table)
d = structure(list(date = structure(rep(c(18690, 18691, 18692, 18693, 18694, 18695, 18696, 18697), 2), class = "Date"),
                   x = c(18, 70, 57, 94, 94, 13, 98, 23, 20, 72, 59, 96, 96, 15, 100, 25),
                   grps = c(rep("g1", 8), rep("g2", 8))),
              row.names = c(NA, -16L), class = c("data.table", "data.frame"))
d[, cmx := cummax(x), by = .(grps)]
d[, scmx := c(18, 18, 57, 70, 70, 70, 94, 94, 20, 20, 59, 72, 72, 72, 96, 96)]

```

---

### Context

If `x` corresponds to a performance rating, what I am trying to do is locate the date when they achieved their best performance and their second best. A similar question of mine where I needed to locate the row which corresponded to the highest cumulative value in a column:

[Fill down first row within each cumulative max, with a twist](https://stackoverflow.com/questions/66472599/fill-down-first-row-within-each-cumulative-max-with-a-twist/66479411?noredirect=1#comment117649792_66479411)