I need help with keeping a variable over rows unless break rules apply.
Here is the dataset I have. Variables ID, qty.
I need to retain qty over rows. 2 rules for breaking the logic would apply:
- A new ID
- 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.