How to add a cumulative column to an R dataframe using dplyr?

前端 未结 1 1319
梦谈多话
梦谈多话 2020-11-27 04:22

I have the same question as this post, but I want to use dplyr:

With an R dataframe, eg:

df <- data.frame(id = rep(1:3, each = 5)
            


        
相关标签:
1条回答
  • 2020-11-27 04:29

    Like this?

    df <- data.frame(id = rep(1:3, each = 5),
                     hour = rep(1:5, 3),
                     value = sample(1:15))
    
    mutate(group_by(df,id), cumsum=cumsum(value))
    

    Or if you use the dplyr's piping operator:

    df %>% group_by(id) %>% mutate(cumsum = cumsum(value))
    

    Result in both cases:

    Source: local data frame [15 x 4]
    Groups: id
    
       id hour value cumsum
    1   1    1     4      4
    2   1    2    14     18
    3   1    3     8     26
    4   1    4     2     28
    5   1    5     3     31
    6   2    1    10     10
    7   2    2     7     17
    8   2    3     5     22
    9   2    4    12     34
    10  2    5     9     43
    11  3    1     6      6
    12  3    2    15     21
    13  3    3     1     22
    14  3    4    13     35
    15  3    5    11     46
    
    0 讨论(0)
提交回复
热议问题