Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

Vector-setops

Set operations on vector-like objects


Description

Perform set operations on Vector objects.

Usage

## S4 method for signature 'Vector,Vector'
union(x, y)

## S4 method for signature 'Vector,Vector'
intersect(x, y)

## S4 method for signature 'Vector,Vector'
setdiff(x, y)

## S4 method for signature 'Vector,Vector'
setequal(x, y)

Arguments

x, y

Vector-like objects.

Details

The union, intersect, and setdiff methods for Vector objects return a Vector object containing respectively the union, intersection, and (asymmetric!) difference of the 2 sets of vector elements in x and y. The setequal method for Vector objects checks for set equality between x and y.

They're defined as follow:

setMethod("union", c("Vector", "Vector"),
        function(x, y) unique(c(x, y))
    )
    setMethod("intersect", c("Vector", "Vector"),
        function(x, y) unique(x[x %in% y])
    )
    setMethod("setdiff", c("Vector", "Vector"),
        function(x, y) unique(x[!(x %in% y)])
    )
    setMethod("setequal", c("Vector", "Vector"),
        function(x, y) all(x %in% y) && all(y %in% x)
    )

so they work out-of-the-box on Vector objects for which c, unique, and %in% are defined.

Value

union returns a Vector object obtained by appending to x the elements in y that are not already in x.

intersect returns a Vector object obtained by keeping only the elements in x that are also in y.

setdiff returns a Vector object obtained by dropping from x the elements that are in y.

setequal returns TRUE if x and y contain the same sets of vector elements and FALSE otherwise.

union, intersect, and setdiff propagate the names and metadata columns of their first argument (x).

Author(s)

Hervé Pagès

See Also

  • Vector-comparison for comparing and ordering vector-like objects.

  • Vector-merge for merging vector-like objects.

  • Vector objects.

  • BiocGenerics::union, BiocGenerics::intersect, and BiocGenerics::setdiff in the BiocGenerics package for general information about these generic functions.

Examples

## See ?`Hits-setops` for some examples.

S4Vectors

Foundation of vector-like and list-like containers in Bioconductor

v0.28.1
Artistic-2.0
Authors
H. Pagès, M. Lawrence and P. Aboyoun
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.