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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…