I have a data.table with two parameters(date and status), now I want to insert new columns based on the original table.
data rules:
- the Status column contains only "0" and "1"
- the Date column is always increase by seconds :)
new variables:
- group: to number each group or cycle for the status, the order of the status is (0,1). it means that the status starts with status '0', when the status becomes '0' again, one cycle is completed.
- cycle_time: calculate the cycle time for each group
- group_0: calculate the time for the status 0 within a specific group
- group_1: calculate the time for the status 1 within a specific group
For example, a simple input:
the code to generate the data:
dd <- data.table(date = c("2015-07-01 00:00:12", "2015-07-01 00:00:13","2015-07-01 00:00:14","2015-07-01 00:00:15", "2015-07-01 00:00:16", "2015-07-01 00:00:17","2015-07-01 00:00:18", "2015-07-01 00:00:19", "2015-07-01 00:00:20","2015-07-01 00:00:21", "2015-07-01 00:00:22", "2015-07-01 00:00:23","2015-07-01 00:00:24", "2015-07-01 00:00:25"), status = c(0,0,0,0,1,1,1,0,0,1,1,1,1,0))
the output including new parameters is:
actually i have done with some basic methods,
- the main idea is :if the current status is 0 and the next status is 1, then mark it as one group.
- the idea could work, but the problem is the calculation time is too long, since so many loops.
I supposed that there could be an easier solution for this case