I have the following data:
| Account | date | type |
|---|---|---|
| 1 | 2021-08-31 | 0 |
| 1 | 2021-09-23 | 0 |
| 1 | 2021-09-30 | 5 |
| 1 | 2021-10-30 | 0 |
| 1 | 2021-12-29 | 0 |
| 1 | 2022-01-31 | 8 |
| 1 | 2022-02-02 | 0 |
I need to find the minimum date of each individual transition.
group_by(Account, type) %>%
summarise(first_appearance = min(date))
returns
| Account | date | type |
|---|---|---|
| 1 | 2021-08-31 | 0 |
| 1 | 2021-09-30 | 5 |
| 1 | 2022-01-31 | 8 |
How can I group by each SHIFT in type?
My initial thoughts are to generate some sort of sequence along the factors and concatenate to have a unique grouping variable, but how would this be done?
| Account | date | type | order | type_order |
|---|---|---|---|---|
| 1 | 2021-08-31 | 0 | A | 0A |
| 1 | 2021-09-23 | 0 | A | 0A |
| 1 | 2021-09-30 | 5 | A | 5A |
| 1 | 2021-10-30 | 0 | B | 0B |
| 1 | 2021-12-29 | 0 | B | 0B |
| 1 | 2022-01-31 | 8 | A | 8A |
| 1 | 2022-02-02 | 0 | C | 0C |
Desired output would be:
group_by(Account, type_order) %>%
summarise(first_appearance = min(date))
| Account | date | type | order | type_order |
|---|---|---|---|---|
| 1 | 2021-08-31 | 0 | A | 0A |
| 1 | 2021-09-30 | 5 | A | 5A |
| 1 | 2021-10-30 | 0 | B | 0B |
| 1 | 2022-01-31 | 8 | A | 8A |
| 1 | 2022-02-02 | 0 | C | 0C |
Maybe use
rleidfromdata.tableto assign groups when there are differences intypefrom row to row.Output