I have a data like this:
data<-data.frame(time=c(20230404001040, 20230404001050,20230404001100, 20230404001110, 20230404001120,20230404001130,
20230404001140,20230404001150,20230404001200),
on=c("FALSE", "FALSE", "FALSE", "TRUE","TRUE","TRUE","FALSE","FALSE","FALSE"))
'time' is written as ymd_hms representation. I think I can use data[,1] <- ymd_hms(data[,1])
.
If on
is FALSE, it means that the switch is off.
If on
is TRUE, it means that the switch is on.
I want to calculate the duration time of each on/off event. Each row of time
is 10-second interval. So I can count how many rows within each on/off event and multiply to 10. So my desired output should look like this:
data<-data.frame(time=c(20230404001040, 20230404001050,20230404001100, 20230404001110, 20230404001120,20230404001130,
20230404001140,20230404001150,20230404001200),
on=c("FALSE", "FALSE", "FALSE", "TRUE","TRUE","TRUE","FALSE","FALSE","FALSE"),
time_after_switch=c(0,10,20,0,10,20,0,10,20))
For my data
first 3 rows are switch off event, next 3 rows are switch on event, finally last 3 rows are switch off event. So I can think of it as 3 cycles. Within each cycle, the duration times are 0,10,20,0,10,20,0,10,20. I want to make r code calculating the values of time_after_switch
.