I'm trying to break down a vector of event times into episodes. An episode must meet 2 criteria. 1) It consists of 3 or more events and 2) those events have inter-event times of 25 time units or less. My data is organized in a data frame as shown below.

So far, I figured out that I can find the difference between events with diff(EventTime). By creating a logical vector that corresponds to events that the 2nd inter-event criterion, I can use `rle(EpisodeTimeCriterion)` to get a the total number, and length of episodes.

```
    EventTime   TimeDifferenceBetweenNextEvent     EpisodeTimeCriterion
    25          NA                                 NA
    75          50                                 TRUE
    100         25                                 TRUE
    101         1                                  TRUE
    105         4                                  TRUE
    157         52                                 FALSE
    158         1                                  TRUE
    160         2                                  TRUE
    167         7                                  TRUE
    169         2                                  TRUE
    170         1                                  TRUE
    175         5                                  TRUE
    178         3                                  TRUE
    278         100                                FALSE
    302         24                                 TRUE
    308         6                                  TRUE
    320         12                                 TRUE
    322         459                                FALSE

```

However, I would like to know the timing of the episodes and 'rle()' doesn’t let me do that.

Ideally I would like to generate a data frame that looks like this:

```
    Episode      EventsPerEpisode   EpisodeStartTime   EpisodeEndTime
    1            4                  75                 105
    2            7                  158                178
    3            3                  302                322

```

I know that this is probably a simple problem but being new to R, the only solution I can envision is some series of loops. Is there a way of doing this without loops? Or is there package that lends itself to this sort of analysis? 

Thanks!

Edited for clarity. Added a desired outcome data fame and expanded the example data to make it clearer.