Loop to sum up observation greater than subject in R

后端 未结 1 1342
逝去的感伤
逝去的感伤 2021-01-14 13:52

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

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-14 14:10

    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)
    

    0 讨论(0)
提交回复
热议问题