I'm having difficulty performing a reservoir balance process, I explain.
I want to calculate the value stored at the end of the month considering input and output (consumption).
EX:
At the beginning (beginning of January) the reservoir is empty, so there is an input of X value and an output of Y value. What remains will be the stored value. So in the month of February there is a new input (X may be different from X in January) and the exit remains constant. The stored value will now be February Input + January Stored Value - output. and so on until the month of December.
With the proviso that if the result is negative it will consider 0 and if it is greater than 16000 it will consider 16000
My data is:
| month | input | Exit | 
|---|---|---|
| jan | 4700 | 2250 | 
| fev | 6990 | 2250 | 
| mar | 8900 | 2250 | 
| abr | 9000 | 2250 | 
| may | 5250 | 2250 | 
| jun | 2790 | 2250 | 
| jul | 1770 | 2250 | 
| ago | 492 | 2250 | 
| set | 89 | 2250 | 
| out | 572 | 2250 | 
| nov | 830 | 2250 | 
| dez | 744 | 2250 | 
expected result:
| month | input | Exit | Stored | 
|---|---|---|---|
| jan | 4700 | 2250 | 2516 | 
| fev | 6990 | 2250 | 7256 | 
| mar | 8900 | 2250 | 13980 | 
| abr | 9000 | 2250 | 16000 | 
| may | 5250 | 2250 | 16000 | 
| jun | 2790 | 2250 | 15688 | 
| jul | 1770 | 2250 | 14677 | 
| ago | 492 | 2250 | 12772 | 
| set | 89 | 2250 | 10584 | 
| out | 572 | 2250 | 8735 | 
| nov | 830 | 2250 | 7066 | 
| dez | 744 | 2250 | 5915 | 
I managed to do it in excel, but I need to automate some things and that's why I'm using R
 
    