set {relations} | R Documentation |
Creation and manipulation of sets.
set(...) as.set(x) is.set(x) set_is_empty(x) set_is_subset(a, b) set_is_proper_subset(a, b) set_is_equal(a, b) set_is_element(e, b) set_union(...) set_intersection(...) set_symdiff(...) set_complement(a, b) set_power(x) set_cartesian(...) set_combn(x, m)
x |
For as.set() and is.set() :
an R object. A set object otherwise. |
a, b |
Set objects. |
e |
An R object. |
m |
Number of elements to choose. |
... |
For set() : R objects, and set objects otherwise. |
These functions represent basic infrastructure for handling sets
of general (R) objects. The set_is_foo()
predicates
are vectorized. In addition
to the methods defined, one can use the following operators:
+
and |
for the union, -
for
the complement, &
for the
intersection, %D%
for the symmetric difference,
*
and ^n
for the
(n-fold) cartesian product, 2^
for the power set,
%e%
for the element-of predicate,
<
and <=
for
the (proper) subset predicate, >
and >=
for
the (proper) superset predicate, and ==
and !=
for
(in)equality. The length
method for sets gives the
cardinality. set_combn
returns the set of all
subsets of specfied length. The Summary
methods do also work if
defined for the set elements. The mean
and
median
methods try to convert the object to a numeric vector before calling
the default methods.
Because set elements are unordered, it is not sensible to use indexing
(except using labels). However, it is possible to iterate over
all elements using for
and {l,s}apply
.
Note that set_union
, set_intersection
, and
set_symdiff
accept any number of arguments. The n-ary
symmetric difference of a collection (or multiset; here: the
list of arguments) of sets contains
just elements which are in an odd number of the sets in the
collection.
set_is_element
is vectorized in e
, that is, if e
is an atomic vector or list, the is-element operation is performed
element-wise, and a logical vector returned. Note that, however,
objects of class "tuple"
are taken as atomic objects to
correctly handle sets of tuples.
set_outer
,
tuple
for tuples (“vectors”), and
relation
.
## constructor s <- set(1,2,3) s ## named elements snamed <- set(one = 1, 2, three = 3) snamed ## named elements can directly be accessed snamed[["one"]] ## a more complex set set(c, "test", list(1, 2, 3)) ## converter s2 <- as.set(2:5) s2 ## set of sets set(set(), set(1)) ## cartesian product s * s2 s * s s ^ 2 # same as above s ^ 3 ## power set 2 ^ s ## tuples s3 <- set(tuple(1,2,3), tuple(2,3,4)) s3 ## Predicates: ## element 1:2 %e% s tuple(1,2,3) %e% s3 ## subset s <= s2 s2 >= s # same ## proper subset s < s ## complement, union, intersection, symmetric difference: s - 1 s + set("a") # or use: s | set("a") s & s s %D% s2 set(1,2,3) - set(1,2) set_intersection(set(1,2,3), set(2,3,4), set(3,4,5)) set_union(set(1,2,3), set(2,3,4), set(3,4,5)) set_symdiff(set(1,2,3), set(2,3,4), set(3,4,5)) ## subsets: set_combn(as.set(1:3),2) ## iterators: sapply(s, sqrt) for (i in s) print(i) ## Summary methods sum(s) range(s) ## mean / median mean(s) median(s)