r-lib / generics
1
#' Set operations
2
#'
3
#' Union (`union()`), intersect (`intersect()`), difference (`setdiff()`),
4
#' and equality (`setequal()`) for two vectors representing sets. Determine
5
#' membership with `is.element()`.
6
#'
7
#' These functions override the set functions provided in base to make them
8
#' generic so that packages can provide methods for different data types. The
9
#' default methods call the base versions.
10
#'
11
#' @section Methods:
12
#'
13
#' \subsection{`intersect()`}{
14
#'     \Sexpr[stage=render,results=rd]{generics:::methods_rd("intersect")}
15
#' }
16
#'
17
#' \subsection{`union()`}{
18
#'    \Sexpr[stage=render,results=rd]{generics:::methods_rd("union")}
19
#' }
20
#'
21
#' \subsection{`setdiff()`}{
22
#'    \Sexpr[stage=render,results=rd]{generics:::methods_rd("setdiff")}
23
#' }
24
#'
25
#' \subsection{`setequal()`}{
26
#'    \Sexpr[stage=render,results=rd]{generics:::methods_rd("setequal")}
27
#' }
28
#'
29
#' \subsection{`is.element()`}{
30
#'    \Sexpr[stage=render,results=rd]{generics:::methods_rd("is.element")}
31
#' }
32
#'
33
#' @param x,y Vectors to combine.
34
#' @param el,set Element and set to compare.
35
#' @param ... Other arguments passed on to methods.
36
#' @return For `union()`, `intersect()`, and `setdiff()`, a vector with all
37
#'   duplicate removed.
38
#'
39
#'   For `setequal()` and `is.element()`,  a logical `TRUE` or `FALSE`.`
40
#' @name setops
41
#' @examples
42
#' intersect(1:5, 4:8)
43
#' union(1:5, 4:8)
44
#'
45
#' setdiff(1:5, 4:8)
46
#' setdiff(4:8, 1:5)
47
NULL
48

49
#' @rdname setops
50
#' @export
51 0
intersect <- function(x, y, ...) UseMethod("intersect")
52
#' @rdname setops
53
#' @export
54 0
union <- function(x, y, ...) UseMethod("union")
55
#' @rdname setops
56
#' @export
57 0
setdiff <- function(x, y, ...) UseMethod("setdiff")
58
#' @rdname setops
59
#' @export
60 0
setequal <- function(x, y, ...) UseMethod("setequal")
61
#' @rdname setops
62
#' @export
63 0
is.element <- function(el, set, ...) UseMethod("is.element")
64

65
#' @export
66 0
intersect.default <- function(x, y, ...) base::intersect(x, y, ...)
67
#' @export
68 0
union.default <- function(x, y, ...) base::union(x, y, ...)
69
#' @export
70 0
setdiff.default <- function(x, y, ...) base::setdiff(x, y, ...)
71
#' @export
72 0
setequal.default <- function(x, y, ...) base::setequal(x, y, ...)
73
#' @export
74 0
is.element.default <- function(el, set, ...) base::is.element(el, set, ...)

Read our documentation on viewing source code .

Loading