I have a data set that looks like that
set.seed(100)
da <- data.frame(exp = c(rep(\"A\", 4), rep(\"B\", 4)), diam = runif(8, 10, 30))
Fo
If you don't require the initial order in the result you could do it quite efficiently like this:
library(data.table)
setorder(setDT(da), exp, -diam)
da[, d2 := cumsum(diam) - diam, by = exp]
da
# exp diam d2
#1: A 21.04645 0.00000
#2: A 16.15532 21.04645
#3: A 15.15345 37.20177
#4: A 11.12766 52.35522
#5: B 26.24805 0.00000
#6: B 19.67541 26.24805
#7: B 19.37099 45.92347
#8: B 17.40641 65.29445
Using dplyr, that would be:
library(dplyr)
da %>%
arrange(exp, desc(diam)) %>%
group_by(exp) %>%
mutate(d2 = cumsum(diam) - diam)