Learn R Programming

sets (version 0.3-2)

cset: Customizable sets

Description

Creation and manipulation of customizable sets.

Usage

cset(gset,
     orderfun = set_options("orderfun"),
     matchfun = set_options("matchfun"))

make_matchfun(FUN)

cset_orderfun(x) cset_matchfun(x) cset_orderfun(x) <- value cset_matchfun(x) <- value

as.cset(x) is.cset(x)

cset_is_empty(x) cset_is_subset(x, y) cset_is_proper_subset(x, y) cset_is_equal(x, y) cset_contains_element(x, e)

cset_is_set(x) cset_is_multiset(x) cset_is_fuzzy_set(x) cset_is_set_or_multiset(x) cset_is_set_or_fuzzy_set(x) cset_is_fuzzy_multiset(x) cset_is_crisp(x)

cset_cardinality(x) cset_union(...) cset_sum(...) cset_difference(...) cset_intersection(...) cset_symdiff(...) cset_complement(x, y) cset_power(x) cset_cartesian(...) cset_combn(x, m)

cset_similarity(x, y, method = "Jaccard")

## S3 method for class 'cset': cut(x, level = 1, \dots) ## S3 method for class 'cset': mean(x, \dots) ## S3 method for class 'cset': median(x, na.rm = FALSE) ## S3 method for class 'cset': length(x)

Arguments

x
For as.cset() and is.cset(): an Robject. A (c)set object otherwise.
y
A (c)set object.
gset
A generalized set (or some other Robject coercible to it).
matchfun
A function for matching similar elements, comparable to match, taking two arguments: x (vector of elements to be matched) and table (vector of elements to be matched aga
FUN
A predicate testing for equality of two objects.
orderfun
A function taking a list and returning an integer vector, specifying the order in which an iterator processes the set elements. Alternatively, the index vector can be specified directly.
value
A new match function (order function).
e
An object of class element.
m
Number of elements to choose.
method
Currently, only "Jaccard" is implemented.
level
The minimum membership level.
na.rm
logical indicating whether NA values should be removed.
...
For cset_foo(): (c)set objects. For the mean and sort methods: additional parameters internally passed to mean and order, respecti

Details

Customizable sets extend generalized sets in two ways: First, users can control the way elements are matched, i.e., define equivalence classes of elements. Second, an order function (or permutation index) can be specified for each set for changing the order in which iterators such as as.list process the elements. The latter in particular influences the labeling and print methods for customizable sets.

The match function needs to be vectorized in a similar way than match. make_matchfun can be used to create such a function from a simple predicate testing for equality (such as, e.g., identical). Make sure, however, to create the same function only once.

Note that operations on customizable sets require the same match function for all sets involved. The order function can differ, but will then be stripped from the result.

set_options can be used to conveniently switch the default match and/or order function if a number of cset objects need to be created.

See Also

set for (ordinary) sets, gset for generalized sets, cset_outer, and tuple for tuples (vectors).

Examples

Run this code
## default behavior of sets: matching of elements is very strict
## Note that on most systems, 3.3 - 2.2 != 1.1
x <- set("1", 1L, 1, 3.3 - 2.2, 1.1)
print(x)

y <- set(1, 1.1, 2L, "2")
print(y)
1L %e% y

set_union(x, y)
set_intersection(x, y)
set_complement(x, y)

## Now use the more sloppy match()-function. 
## Note that 1 == "1" == 1L ...
X <- cset(x, matchfun = match)
print(X)
Y <- cset(y, matchfun = match)
print(Y)
1L %e% Y

cset_union(X, Y)
cset_intersection(X, Y)
cset_complement(X, Y)

## Same using all.equal().
## This is a non-vectorized predicate, so use make_matchfun
## to generate a vectorized version:
FUN <- make_matchfun(function(x, y) isTRUE(all.equal(x, y)))
X <- cset(x, matchfun = FUN)
print(X)
Y <- cset(y, matchfun = FUN)
print(Y)
1L %e% Y

cset_union(X, Y)
cset_intersection(X, Y)
cset_complement(X, Y)

### change default functions via set_option
set_options("matchfun", match)
cset(x)
cset(y)

cset(1:3) <= cset(c(1,2,3))

### restore package defaults
set_options("matchfun", NULL)

### customized order function
FUN <- function(x) order(as.character(x), decreasing = TRUE)
Z <- cset(letters[1:5], orderfun = FUN)
print(Z)
as.character(Z)

## converter for ordered factors keeps order
o <- ordered(c("a", "b", "a"), levels = c("b", "a"))
as.set(o)
as.cset(o)

## converter for other data types keep order if the elements are unique:
as.cset(c("A", "quick", "brown", "fox"))
as.cset(c("A", "quick", "brown", "fox", "quick"))

Run the code above in your browser using DataLab