This should be trivial to code but could not think of an elegant one-liner in R. I have a dataframe as below:
data <- data.frame( index= seq(1:20), event=rep(0,20) )
data$event[10] <- 1
data$event[15] <- 1
I simply want to add start and stop counter columns that increment in 10's and reset right after an event=1 is observed. So the desired output with these two additional columns would be:
index event start stop
1 1 0 0 10
2 2 0 10 20
3 3 0 20 30
4 4 0 30 40
5 5 0 40 50
6 6 0 50 60
7 7 0 60 70
8 8 0 70 80
9 9 0 80 90
10 10 1 90 100
11 11 0 0 10
12 12 0 10 20
13 13 0 20 30
14 14 0 30 40
15 15 1 40 50
16 16 0 0 10
17 17 0 10 20
18 18 0 20 30
19 19 0 30 40
20 20 0 40 50
Obviously, data$stop <- data$start + 10 but how can I apply() the start incrementation loigc as described?
How about this:
Reduce(function(x,y) (1-y)*(x+10), data$event[-nrow(data)], accumulate=T, init=0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With