-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #237 from jmbarbone/120-between
120 between
- Loading branch information
Showing
8 changed files
with
208 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
#' Betwixt boundaries | ||
#' | ||
#' Compare a vector betwitx (between) other values | ||
#' | ||
#' @param x A numeric vector of values | ||
#' @param left,right Boundary values. For [betwixt()], when `NULL` no | ||
#' comparison is made for that boundary. When both are `NULL`, `x` is just | ||
#' returned. | ||
#' | ||
#' @details `type``, `bounds`` can be one of the below: | ||
#' | ||
#' \describe{ | ||
#' \item{g,(}{is greater than (>)} | ||
#' \item{ge,[}{greater than or equal to (>=)} | ||
#' \item{l,))}{less than (<)} | ||
#' \item{le,[]}{less than or equal to (<=)} | ||
#' } | ||
#' | ||
#' Note: [between_more()] may be deprecated in the future in favor of just | ||
#' [betwixt()] | ||
#' | ||
#' @returns A logical vector | ||
#' | ||
#' @examples | ||
#' | ||
#' between_more(2:10, 2, 10, "gl") | ||
#' betwixt(2:10, 2, bounds = "()") | ||
#' between_more(10, 2, 10, "gle") | ||
#' betwixt(2:10, bounds = "(]") | ||
#' betwixt(1:5, c(3, 3, 2, 2, 1), 5) | ||
#' @name betwixt | ||
#' @aliases between betwee_more | ||
NULL | ||
|
||
# TODO consider deprecating `between_more()` in favor of `betwixt()`` | ||
|
||
#' @rdname betwixt | ||
#' @export | ||
#' @param type Abbreviation for the evaluation of `left` on `right` (see | ||
#' details) | ||
between_more <- function(x, left, right, type = c("gele", "gel", "gle", "gl")) { | ||
type <- match_param(type) | ||
|
||
if (any(left > right, na.rm = TRUE)) { | ||
warning(cond_between_more_lr()) | ||
} | ||
|
||
switch( | ||
type, | ||
gele = x >= left & x <= right, | ||
gel = x >= left & x < right, | ||
gle = x > left & x <= right, | ||
gl = x > left & x < right | ||
) | ||
} | ||
|
||
#' @rdname betwixt | ||
#' @export | ||
#' @param bounds Boundaries for comparisons of `left` and `right` (see details) | ||
betwixt <- function( | ||
x, | ||
left = NULL, | ||
right = NULL, | ||
bounds = c("[]", "[)", "(]", "()") | ||
) { | ||
left_null <- is.null(left) | ||
right_null <- is.null(right) | ||
|
||
if (left_null && right_null) { | ||
return(x) | ||
} | ||
|
||
if (any(left > right, na.rm = TRUE)) { | ||
warning(cond_betwixt_lr()) | ||
} | ||
|
||
funs <- switch( | ||
match_param(bounds), | ||
"[]" = c(">=", "<="), | ||
"[)" = c(">=", "<"), | ||
"(]" = c(">", "<="), | ||
"()" = c(">", "<") | ||
) | ||
|
||
if (left_null) { | ||
left <- TRUE | ||
} else { | ||
left <- do.call(funs[1], list(x, left)) | ||
} | ||
|
||
if (right_null) { | ||
right <- TRUE | ||
} else { | ||
right <- do.call(funs[2], list(x, right)) | ||
} | ||
|
||
left & right | ||
} | ||
|
||
# conditions -------------------------------------------------------------- | ||
|
||
cond_between_more_lr <- function() { | ||
new_condition( | ||
"`left` > `right`", | ||
"between_more_lr", | ||
type = "warning" | ||
) | ||
} | ||
|
||
cond_betwixt_lr <- function() { | ||
new_condition( | ||
"`left` > `right`", | ||
"betwixt_lr", | ||
type = "warning" | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters