so I have similar problem like one that was described here: Numbering rows within groups in a data frame
I want to give and id to rows within groups again, but the main difference is that I have a date field, which sometimes has gaps in it. A gap would also indicate, that we have a new group.
Here is my code and ​what I've achieved:
data <- data %>%
arrange(group1, group2, day) %>%
group_by(group1, group2) %>%
mutate(id_day = row_number())
day group1 group2 id_day
2020-05-01 A B 1
2020-05-02 A B 2
2020-05-03 A B 3
2020-05-04 A B 4
2020-05-07 A B 5
2020-05-08 A B 6
2020-05-09 A B 7
2020-06-05 C D 1
2020-06-06 C D 2
2020-06-07 C D 3
2020-06-08 C D 4
2020-06-09 C D 5
2020-06-10 C D 6
2020-06-11 C D 7
And this is what I want to achieve:
day group1 group2 id_day
2020-05-01 A B 1
2020-05-02 A B 2
2020-05-03 A B 3
2020-05-04 A B 4
2020-05-07 A B 1
2020-05-08 A B 2
2020-05-09 A B 3
2020-06-05 C D 1
2020-06-06 C D 2
2020-06-07 C D 3
2020-06-08 C D 4
2020-06-09 C D 5
2020-06-10 C D 6
2020-06-11 C D 7
Any ideas how to do that?
Thanks,