**R – Aaron Schlegel**, and kindly contributed to R-bloggers)

The set operations, union and intersection, the relative complement and the inclusion relation (subsets) are known as the algebra of sets. The algebra of sets can be used to find many identities related to set relations that will be discussed later. We turn now to introducing the relative complement.

###### Relative Complement

The relative complement of two sets and is defined as the members of not in and is denoted (or ). More formally, the relative complement of two sets is defined as:

Just like the set operations union and intersection, the relative complement can be visualized using Venn diagrams.

The shaded area represents the relative complement .

For example, consider the following three sets .

- Let be the set of all Calico cats
- Let be the set of all Manx cats
- Let be the set of all male cats

What are the elements of the set ? Start with the relative complement in parentheses, which is the set of all nonmale Manx cats. It wouldn’t be as correct to state is the set of all female Manx cats as it hasn’t been explicitly defined that the cats not in the set are female. Then the union of and this set is, therefore, the set of all cats who are either Calico or Manx nonmales (or both).

Determining the elements of the set proceeds in the same way. The union of and represents the set of all cats who are Calico or Manx or both. Thus the relative complement of this set is then the set of all nonmale cats who are either or both Calico or Manx.

The set simplifies to one of the sets discussed above. The relative complement is the set of all nonmale Calico cats while is the set of all nonmale Manx cats. The union of these two sets thus results in the set of all nonmale cats who are either Calico or Manx, which is the same as the set .

We can define an R function to find the relative complement of two sets.

relcompFind the relative complements of the sets and

a

print(relcomp(b, a)) ## [1] 7

###### Set Identities

Many identities can be formed using the set operations we have explored.

**Commutative Laws**

We can show this identity using the `isequalset()`

and `set.union()`

functions we created in the previous post on union and intersections.

a

isequalset(set.union(a, b), set.union(b, a)) ## [1] TRUE

isequalset(set.intersection(a, b), set.intersection(b, a)) ## [1] TRUE

**Associative Laws**

Create a third set .

cStarting with the first associative law

assoc.rhsShowing the second associative law,

assoc2.rhs

Distributive Laws

starting with the first distributive law, .

dist.rhsWhich are equal sets as member order does not matter when determining the equality of two sets. The second distributive law, can be demonstrated likewise.

dist2.rhs

De Morgan's Laws

We can use the function to find the relative complement of two sets we wrote earlier to show De Morgan's laws. Starting with the first law,

morgan.rhsThe second De Morgan's law, can be shown similarly.

morgan2.rhsDe Morgan's laws are often stated without , being understood as a fixed set. All sets are a subset of some larger set, which can be called a ‘space,' or . If one considers the space to be the set of all real numbers , and and to be two subsets of (), then De Morgan's laws can be abbreviated as:

We will close the post by stating some identities with the assumption

Though we cannot directly program the set of all real numbers as it is an uncountable set, we can show these identities by using a subset of where a set is a subset of that subset.

Generate the set as the set of integers from one to ten and , our simulated set of all real numbers, as the set of integers from one to 100.

aShow the first identity:

isequalset(set.union(a, s), s) ## [1] TRUE

Second identity:

isequalset(set.intersection(a, s), a) ## [1] TRUE

Third identity:

isequalset(set.union(a, relcomp(s, a)), s) ## [1] TRUE

Fourth identity:

set.intersection(a, relcomp(s, a)) ## logical(0)

###### References

Enderton, H. (1977). Elements of set theory (1st ed.). New York: Academic Press.

The post Algebra of Sets in R appeared first on Aaron Schlegel.

**leave a comment**for the author, please follow the link and comment on their blog:

**R – Aaron Schlegel**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more…

Source:: R News