Retaining variables over rows unless rules of break apply

I need help with keeping a variable over rows unless break rules apply.

Here is the dataset I have. Variables ID, qty.

ID qty
A 1
A 3
B 4
B 2
B 3

I need to retain qty over rows. 2 rules for breaking the logic would apply:

  1. A new ID
  2. Where retained_qty is greater than qty and assign the original qty to continue being retained

ID qty retained_qty
A 1 1
A 3 1
B 4 4
B 2 2 (Since 4 is greater than 2 so need to continue with 2)
C 3 2

I am new to R, have experience with this algorithm in another language but cannot figure out where to begin with R.

Thank you and any help would be really appreciated.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.