Perform set operations on a pair of [number_line]
s.
union_number_lines(x, y)
intersect_number_lines(x, y)
subtract_number_lines(x, y)
[number_line]
; list
union_number_lines()
- Combined the range of x
and that of y
intersect_number_line()
- Subset of x
that overlaps with y
and vice versa
subtract_number_lines()
- Subset of x
that does not overlap with y
and vice versa.
The direction
of the returned [number_line]
will be that of the widest one (x
or y
).
If x
and y
have the same length, it'll be an "increasing"
direction.
If x
and y
do not overlap, NA
("NA ?? NA"
) is returned.
nl_1 <- c(number_line(1, 5), number_line(1, 5), number_line(5, 9))
nl_2 <- c(number_line(1, 2), number_line(2, 3), number_line(0, 6))
# Union
nl_1; nl_2; union_number_lines(nl_1, nl_2)
#> [1] "1 -> 5" "1 -> 5" "5 -> 9"
#> [1] "1 -> 2" "2 -> 3" "0 -> 6"
#> [1] "1 -> 5" "1 -> 5" "0 -> 9"
nl_3 <- number_line(as.Date(c("01/01/2020", "03/01/2020","09/01/2020"), "%d/%m/%Y"),
as.Date(c("09/01/2020", "09/01/2020","25/12/2020"), "%d/%m/%Y"))
nl_4 <- number_line(as.Date(c("04/01/2020","01/01/2020","01/01/2020"), "%d/%m/%Y"),
as.Date(c("05/01/2020","05/01/2020","03/01/2020"), "%d/%m/%Y"))
# Intersect
nl_3; nl_4; intersect_number_lines(nl_3, nl_4)
#> [1] "2020-01-01 -> 2020-01-09" "2020-01-03 -> 2020-01-09"
#> [3] "2020-01-09 -> 2020-12-25"
#> [1] "2020-01-04 -> 2020-01-05" "2020-01-01 -> 2020-01-05"
#> [3] "2020-01-01 -> 2020-01-03"
#> [1] "2020-01-04 -> 2020-01-05" "2020-01-03 -> 2020-01-05"
#> [3] "NA == NA"
# Subtract
nl_3; nl_4; subtract_number_lines(nl_3, nl_4)
#> [1] "2020-01-01 -> 2020-01-09" "2020-01-03 -> 2020-01-09"
#> [3] "2020-01-09 -> 2020-12-25"
#> [1] "2020-01-04 -> 2020-01-05" "2020-01-01 -> 2020-01-05"
#> [3] "2020-01-01 -> 2020-01-03"
#> $n1
#> [1] "2020-01-01 -> 2020-01-04" "2020-01-01 -> 2020-01-03"
#> [3] "NA == NA"
#>
#> $n2
#> [1] "2020-01-05 -> 2020-01-09" "2020-01-05 -> 2020-01-09"
#> [3] "NA == NA"
#>