0

I would like to create a column in my df which would calculate the cumsum according to the id. But when the id changes the sum has to reset to the base value for the given id. So I would like to have something like this:

id   payment    month    year   cumultative_sum

1    100        01       2015   100
1    150        02       2015   250
1    50         03       2015   300
2    75         07       2016   75
2    125        08       2016   200

Right now with my code the cumultative sum keeps adding further instances even if the id changes so it looks like this:

id   payment    month    year   cumultative_sum

1    100        01       2015   100
1    150        02       2015   250
1    50         03       2015   300
2    75         07       2016   375
2    125        08       2016   500

This is my code:

df_ag <- df %>%
    group_by(id) %>%
    arrange(id, year, month) %>%
    mutate(cumultative_sum = cumsum(payment))

What should I change?

Blazej Kowalski
  • 367
  • 1
  • 6
  • 16
  • 2
    I can't reproduce the behavior you describe. Have you tried running this in a clean R session? Is there anything else about your situation that you've left out? – joran Apr 23 '18 at 18:53
  • 5
    You're code is fine, my guess is you loaded `plyr` after `dplyr` and you have [a duplicate of this FAQ](https://stackoverflow.com/q/26106146/903061). – Gregor Thomas Apr 23 '18 at 18:54
  • so you say that the cumsum should reset when the id changes? – Blazej Kowalski Apr 23 '18 at 18:55
  • 5
    We're saying that it actually does reset when we run this code. I agree that Gregor's suggestion is a likely possibility. – joran Apr 23 '18 at 18:56
  • 1
    To summarize the dupe, your simple fix is use `dplyr::mutate` to make sure you use the `dplyr` version of `mutate`, not the `plyr` version., long-term fix is don't load `plyr` (or packages that load `plyr`) after loading `dplyr`. – Gregor Thomas Apr 23 '18 at 19:39
  • thank you guys. restarting session worked – Blazej Kowalski Apr 24 '18 at 16:59

0 Answers0