I have a table that show connections between events:
library(data.table)
df = data.table(p1 = c("x0", "x0", "x1", "x2", "x3"),
p2 = c("x1", "x2", "x3", "x3", "x4"))
Here is an illustration:
The next event may happen only if all previous events have already happened. For example, event x3 may happen only after x1 and x2 irrespective of their sequence.
How can I convert the df table into following one (where all events appear in some permissible order), in a data.table way:
df_required = data.table(p = c("x0", "x1", "x2", "x3", "x4",
"x0", "x1", "x2", "x3", "x4"),
sequence = c(1, 2, 3, 4, 5, 1, 3, 2, 4, 5),
group = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2))
The required table shows two possible groups of connections: x0-x1-x2-x3-x4 and x0-x2-x1-x3-x4. There are two possible ways because two values may immediately follow x0: x1 or x2. The sequence is written above the circles in the illustration as well.