I have a dataframe where the column left
is the leftmost place for the object and the column right
is the most suitable place. I need to group objects if they overlap, or they overlap objects that overlap (recursively). So, for example, if this is my data frame:
left right
0 0 4
1 5 8
2 10 13
3 3 7
4 12 19
5 18 23
6 31 35
so the lines 0
and 3
overlap, so they must be in the same group, as well as the line 1
is overlapping line 3
- so she joins the group.
So, for this example, the output should be something like this:
left right group
0 0 4 0
1 5 8 0
2 10 13 1
3 3 7 0
4 12 19 1
5 18 23 1
6 31 35 2
I thought of different directions, but did not understand (without ugly for
). Any help would be appreciated!