/
match.R
87 lines (78 loc) · 1.96 KB
/
match.R
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#' Value matching - Extensions
#'
#' Non matching alternatives and supplementary functions.
#'
#' @details Contrast with [base::match()], [base::intersect()], and
#' [base::%in%()] The functions of `%wi%` and `%wo%` can be used in lieu of
#' [base::intersect()] and [base::setdiff()]. The primary difference is that
#' the base functions return only unique values, which may not be a desired
#' behavior.
#'
#' @inheritParams base::`%in%`
#' @return
#' * `%out%`: A `logical` vector of equal length of `x`, `table`
#' * `%wo%`, `%wi%`: A vector of values of `x`
#' * `any_match()`, `no_match()`: `TRUE` or `FALSE`
#' * `is_in()`: see [base::%in%()]
#'
#' @examples
#' 1:10 %in% c(1, 3, 5, 9)
#' 1:10 %out% c(1, 3, 5, 9)
#' letters[1:5] %wo% letters[3:7]
#' letters[1:5] %wi% letters[3:7]
#'
#' # base functions only return unique values
#'
#' c(1:6, 7:2) %wo% c(3, 7, 12) # -> keeps duplicates
#' setdiff(c(1:6, 7:2), c(3, 7, 12)) # -> unique values
#'
#' c(1:6, 7:2) %wi% c(3, 7, 12) # -> keeps duplicates
#' intersect(c(1:6, 7:2), c(3, 7, 12)) # -> unique values
#'
#' @export
#' @name match_ext
#' @rdname match_ext
#' @export
is_in <- function(x, table) {
match(x, table, nomatch = 0L) > 0L
}
#' @rdname match_ext
#' @export
is_out <- function(x, table) {
match(x, table, nomatch = 0L) == 0L
}
#' @rdname match_ext
#' @export
`%out%` <- function(x, table) {
is_out(x, table)
}
#' @rdname match_ext
#' @export
is_within <- function(x, table) {
x[x %in% table]
}
#' @rdname match_ext
#' @export
`%wi%` <- function(x, table) {
is_within(x, table)
}
#' @rdname match_ext
#' @export
is_without <- function(x, table) {
x[x %out% table]
}
#' @rdname match_ext
#' @export
`%wo%` <- function(x, table) {
is_without(x, table)
}
#' @rdname match_ext
#' @export
no_match <- function(x, table) {
!any(match(x, table, nomatch = 0L) != 0L)
}
#' @rdname match_ext
#' @export
any_match <- function(x, table) {
any(match(x, table, nomatch = 0L) != 0L)
}