Reset TIME column when AMT> 0
I have a dataframe that looks like this:
ID TIME AMT
1 0 50
1 1 0
1 2 0
1 3 0
1 4 0
1 4 50
1 5 0
1 7 0
1 9 0
1 10 0
1 10 50
The column TIME
in the above data frame is continuous. I want to add another time column that resets the time from zero when AMT>0
. So my output dataframe should look like this:
ID TIME AMT TIME2
1 0 50 0
1 1 0 1
1 2 0 2
1 3 0 3
1 4 0 4
1 4 50 0
1 5 0 1
1 7 0 3
1 9 0 5
1 10 0 6
1 10 50 0
This is mainly achieved by subtracting TIME
from the "fixed" support TIME
with AMT>0
(for example, the reference time for the second AMT>0
is 4. Thus, TIME2
is calculated by subtracting 5-4=1
; 7-4=3
; 9-4=5
etc. How to do this automatically R.
source to share
Originally posted the same answer as @agstudy, so here's an alternative possible R base solution
with(df, ave(TIME, cumsum(AMT > 0L), ID, FUN = function(x) x - x[1L]))
## [1] 0 1 2 3 4 0 1 3 5 6 0
or
library(dplyr)
df %>%
group_by(cumsum(AMT > 0), ID) %>%
mutate(TIME2 = TIME - first(TIME))
source to share