Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make interval equality operator compare start and end of intervals #1136

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ exportMethods("*")
exportMethods("+")
exportMethods("-")
exportMethods("/")
exportMethods("==")
exportMethods("!=")
exportMethods("[")
exportMethods("[<-")
exportMethods("[[")
Expand Down
45 changes: 45 additions & 0 deletions R/intervals.r
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,51 @@ setMethod("%within%", signature(a = "Interval", b = "list"), function(a, b) {
out
})

#' Equality check for Interval objects
#'
#' Check whether `e1` and `e2` have the same start and end date-time objects.
#'
#' @param e1 An Interval object
#' @param e2 Another Interval object
#' @return TRUE if the start and end of `e1` match the start and end of `e1`, otherwise FALSE
#' @method == Interval
#' @export
#' @examples
#' int1 <- interval(as.Date("2021-01-01"), as.Date("2021-12-30"))
#' int2 <- interval(as.Date("2021-01-01"), as.Date("2021-12-30"))
#' int3 <- interval(as.Date("2021-01-02"), as.Date("2021-12-31"))
#'
#' int1 == int2 # TRUE
#' int1 == int3 # FALSE
#' c(int1, int2) == c(int2, int3) # TRUE FALSE
#' int1 == c(int1, int2, int3) # TRUE TRUE FALSE
#' c(int1, int2, int3) == int1 # TRUE TRUE FALSE
setMethod("==", signature(e1 = "Interval", e2 = "Interval"), function(e1, e2) {
int_start(e1) == int_start(e2) & int_end(e1) == int_end(e2)
})

#' Inequality check for Interval objects
#'
#' Check whether `e1` and `e2` have different start or end date-time objects.
#'
#' @param e1 An Interval object
#' @param e2 Another Interval object
#' @return TRUE if the start and end of `e1` do not match the start and end of `e1`, otherwise FALSE
#' @method != Interval
#' @export
#' @examples
#' int1 <- interval(as.Date("2021-01-01"), as.Date("2021-12-30"))
#' int2 <- interval(as.Date("2021-01-01"), as.Date("2021-12-30"))
#' int3 <- interval(as.Date("2021-01-02"), as.Date("2021-12-31"))
#'
#' int1 != int2 # FALSE
#' int1 != int3 # TRUE
#' c(int1, int2) != c(int2, int3) # FALSE TRUE
#' int1 != c(int1, int2, int3) # FALSE FALSE TRUE
#' c(int1, int2, int3) != int1 # FALSE FALSE TRUE
setMethod("!=", signature(e1 = "Interval", e2 = "Interval"), function(e1, e2) {
int_start(e1) != int_start(e2) | int_end(e1) != int_end(e2)
})

#' @export
as.list.Interval <- function(x, ...) {
Expand Down
2 changes: 2 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ reference:
- as.interval
- "`%within%`"
- Interval-class
- "`==,Interval,Interval-method`"
- "`!=,Interval,Interval-method`"

- title: Timespans
desc: >
Expand Down
30 changes: 30 additions & 0 deletions man/equals-Interval-Interval-method.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions man/not-equals-Interval-Interval-method.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions tests/testthat/test-intervals.R
Original file line number Diff line number Diff line change
Expand Up @@ -883,3 +883,35 @@ test_that("Intervals handles missing numbers", {

expect_equal(intersect(int, int), int)
})

test_that("Equality operator checks for matching start and end", {
int1 <- interval(as.Date("2021-01-01"), as.Date("2021-12-30"))
int2 <- interval(as.Date("2021-01-01"), as.Date("2021-12-30"))
int3 <- interval(as.Date("2021-01-02"), as.Date("2021-12-31"))
int4 <- interval(as.Date("2020-12-31"), as.Date("2021-12-30"))

expect_true(int1 == int2)
expect_false(int1 == int3)

expect_equal(c(int1, int2) == c(int2, int1), c(TRUE, TRUE))
expect_equal(c(int1, int2) == c(int3, int4), c(FALSE, FALSE))
expect_equal(c(int1, int2) == c(int2, int3), c(TRUE, FALSE))
expect_equal(int1 == c(int1, int2, int3), c(TRUE, TRUE, FALSE))
expect_equal(c(int1, int2, int3) == int1, c(TRUE, TRUE, FALSE))
})

test_that("Inequality operator checks for non matching start and end", {
int1 <- interval(as.Date("2021-01-01"), as.Date("2021-12-30"))
int2 <- interval(as.Date("2021-01-01"), as.Date("2021-12-30"))
int3 <- interval(as.Date("2021-01-02"), as.Date("2021-12-31"))
int4 <- interval(as.Date("2020-12-31"), as.Date("2021-12-30"))

expect_false(int1 != int2)
expect_true(int1 != int3)

expect_equal(c(int1, int2) != c(int2, int1), c(FALSE, FALSE))
expect_equal(c(int1, int2) != c(int3, int4), c(TRUE, TRUE))
expect_equal(c(int1, int2) != c(int2, int3), c(FALSE, TRUE))
expect_equal(int1 != c(int1, int2, int3), c(FALSE, FALSE, TRUE))
expect_equal(c(int1, int2, int3) != int1, c(FALSE, FALSE, TRUE))
})
Loading