Cumulative amount after 64 in R (notional cumulative amount)
I am new to R. I am not sure how to execute the following function in R. I can do it in Excel. But not able to do it in R. Can anyone help me with this?
I want to get the sum of the counter value when it reaches 64,
Below are my details,
x
57
57
57
57
57
57
58
58
58
58
61
61
62
62
1
1
11
16
16
16
16
16
16
22
22
22
27
28
I want the total amount after the invoice reaches 64. I'm not sure how to do this in R.
Below is the required output,
x
57
57
57
57
57
57
58
58
58
58
61
61
62
62
65
65
76
81
81
81
81
81
81
87
87
87
92
93
Can anyone help with this?
Thanks
source to share
If your data is reset to 64 and continues and you want to keep 64, try:
diffs <- c(dat$x[1], diff(dat$x))
diffs[diffs < 0] <- 64 + diffs[diffs < 0]
cumsum(diffs)
Explanation:
The first line accepts all differences from one number to the next, starting with the initial value (in the example case, 57).
The second line finds all negative diff values ββand changes them to 64+ as they were - if it changed to 2, we need to add 4: 2 to get 64, and then 2 more.
The third line returns a cumsum to give the final values.
source to share