Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
510 views
in Technique[技术] by (71.8m points)

dataframe - Calculate cumulative sum per group in R data.frame (with dplyr)

I have this data.frame:

# A tibble: 20 x 3
         id date     precip
      <dbl> <chr>     <dbl>
 1 59630200 20171225  0    
 2 59630200 20171226  0    
 3 59630200 20171227  0.600
 4 59630200 20171228  0    
 5 59630200 20171229  0    
 6 59630200 20171230  0    
 7 59630200 20171231  0    
 8 59630200 20180101  0.600
 9 49920100 20171225  0    
10 49920100 20171226  0    
11 49920100 20171227  0.900
12 49920100 20171228  0    
13 49920100 20171229  0    
14 49920100 20171230  0    
15 49920100 20171231  0    
16 49920100 20180101  1.60 
17  4530300 20171225  0    
18  4530300 20171226  0.200
19  4530300 20171227  0    
20  4530300 20171228  0   

And I would like to add another column (maybe called cumulated or whatever), where I have the cumulative sum for each id-group. There is an approach described here: R colSums By Group, but I did not manage to make it work. Also I wanted to use dplyr if possible. I though about somehting like:

df %>% 
  group_by(id) %>% 
  mutate(accumulated = colSums(precip))

But this does not work. Maybe someone has an idea:)

question from:https://stackoverflow.com/questions/65922205/calculate-cumulative-sum-per-group-in-r-data-frame-with-dplyr

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...