Skip to contents

merge_episodes() combines overlapping episodes in to a minimal spanning interval split by in individual identifier. Methods are provided for data.frame like objects.

Usage

merge_episodes(x, ...)

# Default S3 method
merge_episodes(x, ...)

# S3 method for class 'data.table'
merge_episodes(x, id = "id", start = "start", end = "end", ...)

# S3 method for class 'tbl_df'
merge_episodes(x, id = "id", start = "start", end = "end", ...)

# S3 method for class 'data.frame'
merge_episodes(x, id = "id", start = "start", end = "end", ...)

Arguments

x

R object.

...

Not currently used.

id

[character]

Variable in x representing the id associated with the episode.

start

[character]

Variable in x representing the start of the episode.

Must refer to a variable that is either class <Date> or <POSIXct>.

end

[character]

Variable in x representing the start of the episode.

Must refer to a variable that is the same class as start.

Value

The resulting combined episode intervals split by id and ordered by interval number.

The returned object will be of the same class as the input x (i.e. a data.frame, data.table or tibble).

Examples

dat <- data.frame(
    id = c(1, 1, 2, 2, 2, 1),
    start = as.Date(c(
        "2020-01-01", "2020-01-03", "2020-04-01",
        "2020-04-15", "2020-04-17", "2020-05-01"
    )),
    end = as.Date(c(
        "2020-01-10", "2020-01-10", "2020-04-30",
        "2020-04-16", "2020-04-19", "2020-10-01"
    ))
)

merge_episodes(dat)
#>   id .interval_number .episode_start .episode_end
#> 1  1                1     2020-01-01   2020-01-10
#> 2  1                2     2020-05-01   2020-10-01
#> 3  2                1     2020-04-01   2020-04-30