this question is a follow-up from two previous ones:

[How to merge two different groupings if they are not disjoint with dplyr](https://stackoverflow.com/questions/63908856/how-to-merge-two-different-groupings-if-they-are-not-disjoint-with-dplyr/63911339?noredirect=1#comment113015621_63911339)

[Assign the same index if two vectors have a common intersection](https://stackoverflow.com/questions/63984825/assign-the-same-index-if-two-vectors-have-a-common-intersection/63988447#63988447)

I want, as in these questions, to assign the same index to observations that are linked, directly or indirectly through a series of groups. For instance, let's say `id1` and `id2`.

But now I wonder if I could somehow add a twist to this process. I still want to link these observations. But the matching following only happens if the final result follows two rules: no matches from `id1` are broken, no pairings between observations with the same date.

Below I show a toy example with expected output:

```
df <- tribble(~Year, ~Quarter, ~ id1, ~ id2,
               2009,  4,         3,     2,
               2010,  2,         3,     2,
               2010,  2,         1,     4,
               2010,  2,         2,     1,
               2010,  3,         1,     3,
               2011,  1,         2,     2,
               2011,  4,         6,     7,
               2012,  1,         5,     2
              ) 

old_way <- tribble(~Year, ~Quarter, ~ id1, ~ id2, ~ id3,
                    2009,  4,         3,     2,     1,
                    2010,  1,         3,     2,     1,
                    2010,  1,         1,     4,     2,
                    2010,  1,         2,     1,     1,
                    2010,  3,         1,     3,     2,
                    2011,  1,         2,     2,     1,
                    2011,  4,         4,     5,     3,
                    2012,  1,         5,     2,     1
                   )

new_way <- tribble(~Year, ~Quarter, ~ id1, ~ id2, ~ id3,
                    2009,  4,         3,     2,     1,
                    2010,  1,         3,     2,     1,
                    2010,  1,         1,     4,     2,
                    2010,  1,         2,     1,     3,
                    2010,  3,         1,     3,     2,
                    2011,  1,         2,     2,     3,
                    2011,  4,         4,     5,     4,
                    2011,  1,         5,     2,     1
                   )

```

Consider `old_way`, in which the date plays no role. Rows 1 and 2 are grouped together because they have the same `id1`. Rows 6 and 8 join them because by having the same `id2`. Finally, row 4 also joins them because it has the same `id1` as row 6.

Rows 3 and 5 are joined to each other due to`id1`. Row 7 doesn't have any intersections.

Going to `new_way`, columns `Year` and `Quarter` enter as blocks to some matches - Rows 2 and 4 can't be matched anymore, since both are `2010/2`.

So, even though row 6 matches rows 1 and 2 by `id2`, it cannot join them, because it has the same `id1` value as row 4, and row 4 has the same date as row 2.

The precedence relation then works as follows: different dates, `id1`, `id2`.