# Algebra of Sets in R

**R – Aaron Schlegel**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The set operations, union and intersection, the relative complement [latex]-[/latex] and the inclusion relation (subsets) [latex]\subseteq[/latex] 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 [latex]A[/latex] and [latex]B[/latex] is defined as the members of [latex]A[/latex] not in [latex]B[/latex] and is denoted [latex]A – B[/latex] (or [latex]A / B[/latex]). More formally, the relative complement of two sets is defined as:

[latex display=”true”] \large{A – B = \{x \in A \space | \space x \notin B \}} [/latex]

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

The shaded area represents the relative complement [latex]A – B[/latex].

For example, consider the following three sets [latex]A, B, C[/latex].

- Let [latex]A[/latex] be the set of all Calico cats
- Let [latex]B[/latex] be the set of all Manx cats
- Let [latex]C[/latex] be the set of all male cats

What are the elements of the set [latex]A \cup (B – C)[/latex]? Start with the relative complement in parentheses, which is the set of all nonmale Manx cats. It wouldn’t be as correct to state [latex]B – C[/latex] is the set of all female Manx cats as it hasn’t been explicitly defined that the cats not in the set [latex]C[/latex] are female. Then the union of [latex]A[/latex] 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 [latex](A \cup B) – C[/latex] proceeds in the same way. The union of [latex]A[/latex] and [latex]B[/latex] represents the set of all cats who are Calico or Manx or both. Thus the relative complement of this set [latex]C[/latex] is then the set of all nonmale cats who are either or both Calico or Manx.

The set [latex](A – C) \cup (B – C)[/latex] simplifies to one of the sets discussed above. The relative complement [latex]A – C[/latex] is the set of all nonmale Calico cats while [latex]B – C[/latex] 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 [latex](A \cup B) – C[/latex].

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

relcomp <- function(a, b) { comp <- vector() for (i in a) { if (i %in% a && !(i %in% b)) { comp <- append(comp, i) } } return(comp) }

Find the relative complements of the sets [latex]A = \{1,2,3,4,5\}[/latex] and [latex]B = \{1,3,5,7\}[/latex]

a <- c(1,2,3,4,5) b <- c(1,3,5,7) print(relcomp(a, b)) ## [1] 2 4 print(relcomp(b, a)) ## [1] 7

###### Set Identities

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

**Commutative Laws**

[latex display="true"] \large{A \cup B = B \cup A} [/latex]

[latex display="true"] \large{A \cap B = B \cap A} [/latex]

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

and `set.union()`

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

a <- c(1,2,3,4,5) b <- c(1,3,5,7) isequalset(set.union(a, b), set.union(b, a)) ## [1] TRUE isequalset(set.intersection(a, b), set.intersection(b, a)) ## [1] TRUE

**Associative Laws**

[latex display="true"] \large{A \cup (B \cup C) = (A \cup B) \cup C} [/latex]

[latex display="true"] \large{A \cap (B \cap C) = (A \cap B) \cap C} [/latex]

Create a third set [latex]c[/latex].

c <- c(2,3,4,6)

Starting with the first associative law [latex]A \cup (B \cup C) = (A \cup B) \cup C[/latex]

assoc.rhs <- set.union(a, set.union(b, c)) # Right-hand Side assoc.lhs <- set.union(set.union(a, b), c) # Left-hand Side print(rbind(assoc.rhs, assoc.lhs)) ## [,1] [,2] [,3] [,4] [,5] [,6] [,7] ## assoc.rhs 1 2 3 4 5 7 6 ## assoc.lhs 1 2 3 4 5 7 6

Showing the second associative law, [latex]A \cap (B \cap C) = (A \cap B) \cap C[/latex]

assoc2.rhs <- set.intersection(a, set.intersection(b, c)) assoc2.lhs <- set.intersection(set.intersection(a, b), c) print(rbind(assoc2.rhs, assoc2.lhs)) ## [,1] ## assoc2.rhs 3 ## assoc2.lhs 3

**Distributive Laws**

[latex display="true"] \large{A \cap (B \cup C) = (A \cap B) \cup (A \cap C)} [/latex]

[latex display="true"] \large{A \cup (B \cap C) = (A \cup B) \cap (A \cup C)} [/latex]

starting with the first distributive law, [latex]A \cap (B \cup C) = (A \cap B) \cup (A \cap C)[/latex].

dist.rhs <- set.intersection(a, set.union(b, c)) dist.lhs <- set.union(set.intersection(a, b), set.intersection(a, c)) print(rbind(dist.rhs, dist.lhs)) ## [,1] [,2] [,3] [,4] [,5] ## dist.rhs 1 2 3 4 5 ## dist.lhs 1 3 5 2 4

Which are equal sets as member order does not matter when determining the equality of two sets. The second distributive law, [latex]A \cup (B \cap C) = (A \cup B) \cap (A \cup C)[/latex] can be demonstrated likewise.

dist2.rhs <- set.union(a, set.intersection(b, c)) dist2.lhs <- set.intersection(set.union(a, b), set.union(a, c)) print(rbind(dist2.rhs, dist2.lhs)) ## [,1] [,2] [,3] [,4] [,5] ## dist2.rhs 1 2 3 4 5 ## dist2.lhs 1 2 3 4 5

**De Morgan’s Laws**

[latex display="true"] \large{C - (A \cup B) = (C - A) \cap (C - B)} [/latex]

[latex display="true"] \large{C - (A \cap B) = (C - A) \cup (C - B)} [/latex]

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, [latex]C - (A \cup B) = (C - A) \cap (C - B)[/latex]

morgan.rhs <- relcomp(c, set.union(a, b)) morgan.lhs <- set.intersection(relcomp(c, a), relcomp(c, b)) print(rbind(morgan.rhs, morgan.lhs)) ## [,1] ## morgan.rhs 6 ## morgan.lhs 6

The second De Morgan’s law, [latex]C - (A \cap B) = (C - A) \cup (C - B)[/latex] can be shown similarly.

morgan2.rhs <- relcomp(c, set.intersection(a, b)) morgan2.lhs <- set.union(relcomp(c, a), relcomp(c, b)) print(rbind(morgan2.rhs, morgan2.lhs)) ## [,1] [,2] [,3] ## morgan2.rhs 2 4 6 ## morgan2.lhs 6 2 4

De Morgan’s laws are often stated without [latex]C[/latex], being understood as a fixed set. All sets are a subset of some larger set, which can be called a ‘space,’ or [latex]S[/latex]. If one considers the space to be the set of all real numbers [latex]\mathbb{R}[/latex], and [latex]A[/latex] and [latex]B[/latex] to be two subsets of [latex]S[/latex] ([latex]\mathbb{R}[/latex]), then De Morgan’s laws can be abbreviated as:

[latex display="true"] \large{-(A \cup B) = - A \cap - B} [/latex]

[latex display="true"] \large{-(A \cap B) = - A \cup - B} [/latex]

We will close the post by stating some identities with the assumption [latex]A \subseteq S[/latex]

[latex display="true"] \large{A \cup S = S \qquad A \cap S = A} [/latex]

[latex display="true"] \large{A \cup - A = S \qquad A \cap - A = \varnothing} [/latex]

Though we cannot directly program the set of all real numbers [latex]\mathbb{R}[/latex] as it is an uncountable set, we can show these identities by using a subset of [latex]\mathbb{R}[/latex] where a set [latex]A[/latex] is a subset of that subset.

Generate the set [latex]A[/latex] as the set of integers from one to ten and [latex]S[/latex], our simulated set of all real numbers, as the set of integers from one to 100.

a <- seq.int(10) s <- seq.int(100)

Show the first identity: [latex]A \cup S = S[/latex]

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

Second identity: [latex]A \cap S = A[/latex]

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

Third identity: [latex]A \cup - A = S[/latex]

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

Fourth identity: [latex]A \cap - A = \varnothing[/latex]

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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.