Learn R Programming

sets (version 0.1-1)

gset: Generalized sets

Description

Creation and manipulation of generalized sets.

Usage

gset(support, memberships, charfun, elements)
as.gset(x)
is.gset(x)

gset_is_empty(x) gset_is_subset(x, y) gset_is_proper_subset(x, y) gset_is_equal(x, y) gset_contains_element(x, e)

gset_is_multiset(x) gset_is_set(x) gset_is_fuzzy_set(x) gset_is_fuzzy_multiset(x) gset_is_crisp(x)

gset_cardinality(x) gset_union(...) gset_sum(...) gset_difference(x, y) gset_intersection(...) gset_symdiff(...) gset_complement(x, y) gset_power(x) gset_cartesian(...) gset_combn(x, m)

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

e(x, memberships = 1L)

## S3 method for class 'gset': sort(x, decreasing = FALSE, \dots)

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

Arguments

x
For e(), as.gset() and is.gset(): an Robject. A (g)set object otherwise.
y
A (g)set object.
e
An Robject.
m
Number of elements to choose.
support
A set of elements giving the support of the gset.
memberships
For an ordinary set: 1 (or simply missing). For a fuzzy set: a value between 0 and 1. For a multiset: a positive integer. For a fuzzy multiset: a multiset with elements from the unit interval. Otherwise, the argument will be transformed
elements
A set (or list) of e objects which are object/memberships-pairs.
charfun
A function taking an object and returning the membership.
method
Currently, only Jaccard is implemented.
level
The minimum membership level.
na.rm
logical indicating whether NA values should be removed.
decreasing
Logical indicating the sorting direction.
...
For gset(): Robjects, and set objects otherwise.

Details

These functions represent basic infrastructure for handling generalized sets of general (R) objects.

A generalized set (or gset) is set of pairs $(e, f)$, where $e$ is some set element and $f$ is the characteristic (or membership) function. For ordinary sets $f$ maps to ${0, 1}$, for fuzzy sets into the unit interval, for multisets into the natural numbers, and for fuzzy multisets $f$ maps to the set of multisets over the unit interval. The gset_is_foo() predicates are vectorized. In addition to the methods defined, one can use the following operators: | for the union, & for the intersection, + for the sum, - for the difference, %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 <=< code=""> for the (proper) subset predicate, > and >= for the (proper) superset predicate, and == and != for (in)equality. The length method for gsets gives the cardinality. set_combn returns the gset 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. The sort method internally rearranges the elements to produce a sorted output. The cut method filters all elements with membership not less then level --- the result, thus, is a crisp (multi)set. gset_similarity computes the simple Jaccard similarity between two generalized sets $A$ and $B$, i.e., the cardinality of the intersection divided by the cardinality of the union of $A$ and $B$.

Because set elements are unordered, it is not allowed to use indexing (except using labels). However, it is possible to iterate over all elements using for and lapply/sapply.

Note that gset_union, gset_intersection, gset_sum and gset_symdiff accept any number of arguments.

gset_contains_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.

See Also

set for ordinary sets, gset_outer, and tuple for tuples (vectors).

Examples

Run this code
## multisets
A = gset(letters[1:5], memberships = c(3, 2, 1, 1, 1))
B = gset(c("a", "c", "e", "f"), memberships = c(2, 2, 1, 2))
cut(B, 0.5)
rep(B, 2)

gset_union(A, B)
gset_intersection(A, B)
gset_complement(A, B)

gset_is_multiset(A)
gset_sum(A, B)
gset_difference(A, B)

## fuzzy sets
A = gset(letters[1:5], memberships = c(1, 0.3, 0.8, 0.6, 0.2))
B = gset(c("a", "c", "e", "f"), memberships = c(0.7, 1, 0.4, 0.9))
A * B

## fuzzy multisets
A = gset(c("a", "b", "d"),
         memberships = list(c(0.3, 1, 0.5), c(0.9, 0.1), gset(c(0.4, 0.7), c(1, 2))))
B = gset(c("a", "c", "d", "e"),
         memberships = list(c(0.6, 0.7), c(1, 0.3), c(0.4, 0.5), 0.9))
gset_union(A, B)
gset_intersection(A, B)
gset_complement(A, B)

## other operations
mean(gset(1:3, c(0.1,0.5,0.9)))
median(gset(1:3, c(0.1,0.5,0.9)))

Run the code above in your browser using DataLab