sets: Set Operations in R
Overview
sets is an R package that provides a comprehensive set of functions
for performing set operations, inspired by Python’s set methods. This
package aims to make set operations in R intuitive and accessible,
especially for users familiar with Python. The functions in sets
leverage the data.table and checkmate packages to ensure efficient
and reliable set operations.
Installation
You can install the sets package directly from GitHub.
# Install devtools if you haven't already install.packages("devtools") # Install sets package from GitHub devtools::install_github("pythonicr/sets")
Getting Started
Basic Usage
Here are some examples demonstrating how to use the functions provided by the sets package.
Compute the Difference Between Sets
The sets_difference function returns the elements in x that are not
in the other sets.
library(sets) # Compute the difference between sets difference <- sets_difference(c(1, 2, 3), c(2, 3, 4)) print(difference) #> [1] 1
Check if Two Sets are Equal
The sets_equal function checks if two sets x and y contain the
same elements.
# Check if sets are equal equal <- sets_equal(c(1, 2, 3), c(3, 1, 2)) print(equal) #> [1] TRUE
Compute the Intersection of Sets
The sets_intersection function returns the common elements in all
sets.
# Compute the intersection of sets intersection <- sets_intersection(c(1, 2, 3), c(2, 3, 4), c(3, 4, 5)) print(intersection) #> [1] 3
Check if Sets are Disjoint
The sets_isdisjoint function checks if two or more sets have no
elements in common.
# Check if sets are disjoint disjoint1 <- sets_isdisjoint(c(1, 2, 3), c(4, 5, 6)) disjoint2 <- sets_isdisjoint(c(1, 2, 3), c(3, 4, 5)) print(disjoint1) #> [1] TRUE print(disjoint2) #> [1] FALSE
Check if a Set is a Subset of Another Set
The sets_issubset function checks if all elements of x are in y.
# Check if a set is a subset subset <- sets_issubset(c(1, 2), c(1, 2, 3)) print(subset) #> [1] TRUE
Check if a Set is a Superset of Another Set
The sets_issuperset function checks if all elements of y are in x.
# Check if a set is a superset superset <- sets_issuperset(c(1, 2, 3), c(2, 3)) print(superset) #> [1] TRUE
Compute the Symmetric Difference Between Sets
The sets_symmetric_difference function returns elements in either x
or y but not in both.
# Compute the symmetric difference between sets symmetric_difference <- sets_symmetric_difference(c(1, 2, 3), c(3, 4, 5)) print(symmetric_difference) #> [1] 1 2 4 5
Compute the Union of Sets
The sets_union function returns all elements present in any of the
sets.
# Compute the union of sets union <- sets_union(c(1, 2, 3), c(3, 4, 5)) print(union) #> [1] 1 2 3 4 5
Contributing
We welcome contributions to the sets package. If you have suggestions, bug reports, or want to contribute code, please open an issue or submit a pull request on our GitHub repository.
- isdisjoint
- issubset
- issuperset
- union
- intersection
- difference
- symmetric_difference
License
sets is released under the MIT License. See the LICENSE file in the package’s repository for more details.