time_overlap: Create a variable based on time overlaps

Description Usage Arguments Examples

View source: R/time_overlap.R

Description

Creates a sequence number for each set of overlapping intervals

Usage

1

Arguments

start

start of the interval. Can be date or numeric, but must be the same as end

end

end of the interval. Can be date or numeric, but must be the same as start

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
tbl <- tibble::tribble(
  ~id,          ~t1,          ~t2,
  "A", "01/01/2020", "04/01/2020",
  "A", "03/01/2020", "15/01/2020",
  "A", "01/02/2020", "15/02/2020",
  "A", "17/02/2020", "23/02/2020",
  "B", "13/02/2020", "18/02/2020",
  "B", "16/02/2020", "20/02/2020"
) %>%
  dplyr::mutate_at(dplyr::vars(t1, t2), lubridate::dmy)

plot(
  x = tbl$t1, y = 1:nrow(tbl), col = (tbl$id == "A") + 1,
  xlim = range(tbl$t1, tbl$t2)
)
points(x = tbl$t2, y = 1:nrow(tbl), col = (tbl$id == "A") + 1)
for (i in 1:nrow(tbl)) {
  lines(x = c(tbl$t1[i], tbl$t2[i]), y = c(i, i), col = (tbl$id[i] == "A") + 1)
}

tbl$seq <- time_overlap(tbl$t1, tbl$t2)

tbl %>%
  dplyr::group_by(id) %>%
  dplyr::mutate(seq = time_overlap(t1, t2))

MyKo101/mutils documentation built on July 28, 2020, 8:52 p.m.