I have a dataset of event days (in a date format), and each event belongs to an (unknown) episode. I want to categorize the events into episodes in such a way that all events within 180 days of the first day of the episode are considered part of the same episode, and the first day of the episode is assigned to all events part of that episode. For example, given a vector of event dates

```
event_dates <- c(34, 102, 190, 202, 245, 460, 500, 517)

```

I'm hoping to get a vector of episode IDs

```
c(1,  1,   1,   1,   2,   3,   3,   3)

```

and a vector of episode start days

```
c(34, 34,  34,  34,  245, 460, 460, 460)

```

This 5th entry begins a new episode because it is more than 180 days after the first date of the first episode; the 6th entry begins a new episode because it is more than 180 days after the first date of the second episode, etc.

I have do perform this operation on millions of separate patients, so ideally I would prefer a vectorized solution that could work with `by` in a `data.table` or in grouped `tibble` even if it is a bit opaque over a readable but slow solution, which I currently have. Thanks!