Skip to contents

add_parent_interval() calculates the minimum spanning interval that contains overlapping episodes and adds this to the input. Methods are provided for data.frame like objects.

Usage

add_parent_interval(x, ...)

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

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

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

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

Arguments

x

R object.

...

Not currently used.

id

[character]

Variable in x representing the id associated with an 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 input data with additional columns for the corresponding parent interval (split across id values).

Additional columns will be labelled '.parent_start', '.parent_end' and '.interval_number' where the interval number is in order of occurrence of the corresponding parent interval.

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"
    ))
)

add_parent_interval(dat)
#>   id      start        end .parent_start .parent_end .interval_number
#> 1  1 2020-01-01 2020-01-10    2020-01-01  2020-01-10                1
#> 2  1 2020-01-03 2020-01-10    2020-01-01  2020-01-10                1
#> 3  2 2020-04-01 2020-04-30    2020-04-01  2020-04-30                1
#> 4  2 2020-04-15 2020-04-16    2020-04-01  2020-04-30                1
#> 5  2 2020-04-17 2020-04-19    2020-04-01  2020-04-30                1
#> 6  1 2020-05-01 2020-10-01    2020-05-01  2020-10-01                2