By Aaron Schlegel
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.
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.
Find the relative complements of the sets and
##  7
Many identities can be formed using the set operations we have explored.
We can show this identity using the
set.union() functions we created in the previous post on union and intersections.
isequalset(set.union(a, b), set.union(b, a))
##  TRUE
isequalset(set.intersection(a, b), set.intersection(b, a))
##  TRUE
Create a third set .
Starting with the first associative law
Showing the second associative law,
starting with the first distributive law, .
Which are equal sets as member order does not matter when determining the equality of two sets. The second distributive law, can be demonstrated likewise.
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,
The second De Morgan's law, can be shown similarly.
De 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.
Show the first identity:
isequalset(set.union(a, s), s)
##  TRUE
isequalset(set.intersection(a, s), a)
##  TRUE
isequalset(set.union(a, relcomp(s, a)), s)
##  TRUE
set.intersection(a, relcomp(s, a))
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.
To leave a comment
for the author, please follow the link and comment on their blog: R – Aaron Schlegel
offers daily e-mail updates
news and tutorials
on topics such as: Data science
, Big Data, R jobs
, visualization (ggplot2
), programming (RStudio
, Web Scraping
) statistics (regression
, time series
) and more…
Source:: R News