<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/set-operations-unions-and-intersections-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.aaronschlegel.com/set-operations-unions-intersections-r/"> R – Aaron Schlegel</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>Part 2 of 2 in the series <a rel="nofollow" href="http://www.aaronschlegel.com/series/set-theory/" title="Set Theory" target="_blank">Set Theory</a></div>
<p>The set operations of unions and intersections should ring a bell for those who’ve worked with relational databases and Venn Diagrams. The ‘union’ of two of sets <img alt="A" title="A" src="https://s0.wp.com/latex.php?latex=A&bg=ffffff&fg=000&s=0" class="latex"> and <img alt="B" title="B" src="https://s0.wp.com/latex.php?latex=B&bg=ffffff&fg=000&s=0" class="latex"> represents a set that comprises all members of <img alt="A" title="A" src="https://s0.wp.com/latex.php?latex=A&bg=ffffff&fg=000&s=0" class="latex"> and <img alt="B" title="B" src="https://s0.wp.com/latex.php?latex=B&bg=ffffff&fg=000&s=0" class="latex"> (or both).</p>
<p>One of the most natural ways to visualize set unions and intersections is using Venn diagrams.</p>
<p><a rel="nofollow" href="http://www.aaronschlegel.com/?attachment_id=22525" target="_blank"><img sizes="(max-width: 800px) 100vw, 800px" src="https://i2.wp.com/www.aaronschlegel.com/wp-content/uploads/venn.png?resize=800%2C362" alt="Venn diagrams visualizing union and intersection operations" class="wp-image-22525" data-recalc-dims="1"></a></p>
<p>The Venn diagram on the left visualizes a set union while the Venn diagram on the right visually represents a set intersection operation.</p>
<h6>Set Unions</h6>
<p>The union of two sets <img alt="A" title="A" src="https://s0.wp.com/latex.php?latex=A&bg=ffffff&fg=000&s=0" class="latex"> and <img alt="B" title="B" src="https://s0.wp.com/latex.php?latex=B&bg=ffffff&fg=000&s=0" class="latex"> is denoted as:</p>
<img alt=" \large{A \cup B} " title=" \large{A \cup B} " src="https://s0.wp.com/latex.php?latex=+%5Clarge%7BA+%5Ccup+B%7D+&bg=ffffff&fg=000&s=0" class="latex">
<p>The union axiom states for two sets <img alt="A" title="A" src="https://s0.wp.com/latex.php?latex=A&bg=ffffff&fg=000&s=0" class="latex"> and <img alt="B" title="B" src="https://s0.wp.com/latex.php?latex=B&bg=ffffff&fg=000&s=0" class="latex">, there is a set whose members consist entirely of those belonging to sets <img alt="A" title="A" src="https://s0.wp.com/latex.php?latex=A&bg=ffffff&fg=000&s=0" class="latex"> or <img alt="B" title="B" src="https://s0.wp.com/latex.php?latex=B&bg=ffffff&fg=000&s=0" class="latex">, or both. More formally, the union axiom is stated as:</p>
<img alt=" \large{\forall a \space \forall b \space \exists B \space \forall x (x \in B \Leftrightarrow x \in a \space \vee \space x \in b)} " title=" \large{\forall a \space \forall b \space \exists B \space \forall x (x \in B \Leftrightarrow x \in a \space \vee \space x \in b)} " src="https://s0.wp.com/latex.php?latex=+%5Clarge%7B%5Cforall+a+%5Cspace+%5Cforall+b+%5Cspace+%5Cexists+B+%5Cspace+%5Cforall+x+%28x+%5Cin+B+%5CLeftrightarrow+x+%5Cin+a+%5Cspace+%5Cvee+%5Cspace+x+%5Cin+b%29%7D+&bg=ffffff&fg=000&s=0" class="latex">
<p>For example, for two sets <img alt="A" title="A" src="https://s0.wp.com/latex.php?latex=A&bg=ffffff&fg=000&s=0" class="latex"> and <img alt="B" title="B" src="https://s0.wp.com/latex.php?latex=B&bg=ffffff&fg=000&s=0" class="latex">:</p>
<img alt=" \large{A = \{3, 5, 7, 11 \} \qquad B = \{3, 5, 13, 17 \}} " title=" \large{A = \{3, 5, 7, 11 \} \qquad B = \{3, 5, 13, 17 \}} " src="https://s0.wp.com/latex.php?latex=+%5Clarge%7BA+%3D+%5C%7B3%2C+5%2C+7%2C+11+%5C%7D+%5Cqquad+B+%3D+%5C%7B3%2C+5%2C+13%2C+17+%5C%7D%7D+&bg=ffffff&fg=000&s=0" class="latex">
<p>The union of the two sets is:</p>
<img alt=" \large{A \cup B = \{3, 5, 7, 11 \} \cup \{3, 5, 13, 17 \} = \{3, 5, 7, 11, 13, 17 \}} " title=" \large{A \cup B = \{3, 5, 7, 11 \} \cup \{3, 5, 13, 17 \} = \{3, 5, 7, 11, 13, 17 \}} " src="https://s0.wp.com/latex.php?latex=+%5Clarge%7BA+%5Ccup+B+%3D+%5C%7B3%2C+5%2C+7%2C+11+%5C%7D+%5Ccup+%5C%7B3%2C+5%2C+13%2C+17+%5C%7D+%3D+%5C%7B3%2C+5%2C+7%2C+11%2C+13%2C+17+%5C%7D%7D+&bg=ffffff&fg=000&s=0" class="latex">
<p>We can define a simple function in R that implements the set union operation. There is a function in base R <code>union()</code> that performs the same operation that is recommended for practical uses.</p>
<pre>
set.union &lt;- function(a, b) {
  u &lt;- a
  for (i in 1:length(b)) {
    if (!(b[i] %in% u)) {
      u &lt;- append(u, b[i])
    }
  }
  return(u)
}
</pre>
<p>Using our function to perform a union operation of the two sets as above.</p>
<pre>
a &lt;- c(3, 5, 7, 11)
b &lt;- c(3, 5, 13, 17)

set.union(a, b)
## [1]  3  5  7 11 13 17
</pre>
<h6>Set Intersections</h6>
<p>The intersection of two sets <img alt="A" title="A" src="https://s0.wp.com/latex.php?latex=A&bg=ffffff&fg=000&s=0" class="latex"> and <img alt="B" title="B" src="https://s0.wp.com/latex.php?latex=B&bg=ffffff&fg=000&s=0" class="latex"> is the set that comprises the elements that are both members of the two sets. Set intersection is denoted as:</p>
<img alt=" \large{A \cap B} " title=" \large{A \cap B} " src="https://s0.wp.com/latex.php?latex=+%5Clarge%7BA+%5Ccap+B%7D+&bg=ffffff&fg=000&s=0" class="latex">
<p>Interestingly, there is no axiom of intersection unlike for set union operations. The concept of set intersection arises from a different axiom, the axiom schema of specification, which asserts the existence of a subset of a set given a certain condition. Defining this condition (also known as a sentence) as <img alt="\sigma(x)" title="\sigma(x)" src="https://s0.wp.com/latex.php?latex=%5Csigma%28x%29&bg=ffffff&fg=000&s=0" class="latex">, the axiom of specification (subset) is stated as:</p>
<img alt=" \large{\forall A \space \exists B \space \forall x (x \in B \Leftrightarrow x \in A \wedge \sigma(x))} " title=" \large{\forall A \space \exists B \space \forall x (x \in B \Leftrightarrow x \in A \wedge \sigma(x))} " src="https://s0.wp.com/latex.php?latex=+%5Clarge%7B%5Cforall+A+%5Cspace+%5Cexists+B+%5Cspace+%5Cforall+x+%28x+%5Cin+B+%5CLeftrightarrow+x+%5Cin+A+%5Cwedge+%5Csigma%28x%29%29%7D+&bg=ffffff&fg=000&s=0" class="latex">
<p>Put another way; the axiom states that for a set <img alt="A" title="A" src="https://s0.wp.com/latex.php?latex=A&bg=ffffff&fg=000&s=0" class="latex"> and a condition (sentence) <img alt="\sigma" title="\sigma" src="https://s0.wp.com/latex.php?latex=%5Csigma&bg=ffffff&fg=000&s=0" class="latex"> of a subset of <img alt="A" title="A" src="https://s0.wp.com/latex.php?latex=A&bg=ffffff&fg=000&s=0" class="latex">, the subset does indeed exist. This axiom leads us to the definition of set intersections without needing to state any additional axioms. Using the subset axiom as a basis, we can define the existence of the set intersection operation. Given two sets <img alt="a" title="a" src="https://s0.wp.com/latex.php?latex=a&bg=ffffff&fg=000&s=0" class="latex"> and <img alt="b" title="b" src="https://s0.wp.com/latex.php?latex=b&bg=ffffff&fg=000&s=0" class="latex">:</p>
<img alt=" \large{\forall a \space \forall b \exists B \space \forall x (x \in B \Leftrightarrow x \in a \space \wedge \space x \in b)} " title=" \large{\forall a \space \forall b \exists B \space \forall x (x \in B \Leftrightarrow x \in a \space \wedge \space x \in b)} " src="https://s0.wp.com/latex.php?latex=+%5Clarge%7B%5Cforall+a+%5Cspace+%5Cforall+b+%5Cexists+B+%5Cspace+%5Cforall+x+%28x+%5Cin+B+%5CLeftrightarrow+x+%5Cin+a+%5Cspace+%5Cwedge+%5Cspace+x+%5Cin+b%29%7D+&bg=ffffff&fg=000&s=0" class="latex">
<p>Stated plainly, given sets <img alt="a" title="a" src="https://s0.wp.com/latex.php?latex=a&bg=ffffff&fg=000&s=0" class="latex"> and <img alt="b" title="b" src="https://s0.wp.com/latex.php?latex=b&bg=ffffff&fg=000&s=0" class="latex">, there exists a set <img alt="B" title="B" src="https://s0.wp.com/latex.php?latex=B&bg=ffffff&fg=000&s=0" class="latex"> that contains the members existing in both sets.</p>
<p>For example, using the previous sets defined earlier:</p>
<img alt=" \large{A = \{3, 5, 7, 11 \} \qquad B = \{3, 5, 13, 17 \}} " title=" \large{A = \{3, 5, 7, 11 \} \qquad B = \{3, 5, 13, 17 \}} " src="https://s0.wp.com/latex.php?latex=+%5Clarge%7BA+%3D+%5C%7B3%2C+5%2C+7%2C+11+%5C%7D+%5Cqquad+B+%3D+%5C%7B3%2C+5%2C+13%2C+17+%5C%7D%7D+&bg=ffffff&fg=000&s=0" class="latex">
<p>The intersection of the two sets is:</p>
<img alt=" \large{A \cap B = \{3, 5, 7, 11 \} \cap \{3, 5, 13, 17 \} = \{3, 5 \}} " title=" \large{A \cap B = \{3, 5, 7, 11 \} \cap \{3, 5, 13, 17 \} = \{3, 5 \}} " src="https://s0.wp.com/latex.php?latex=+%5Clarge%7BA+%5Ccap+B+%3D+%5C%7B3%2C+5%2C+7%2C+11+%5C%7D+%5Ccap+%5C%7B3%2C+5%2C+13%2C+17+%5C%7D+%3D+%5C%7B3%2C+5+%5C%7D%7D+&bg=ffffff&fg=000&s=0" class="latex">
<p>We can also define a straightforward function to implement the set intersection operation. Base R also features a function <code>intersect()</code> that performs the set intersection operation.</p>
<pre>
set.intersection &lt;- function(a, b) {
  intersect &lt;- vector()
  
  for (i in 1:length(a)) {
    if (a[i] %in% b) {
      intersect &lt;- append(intersect, a[i])
    }
  }
  return(intersect)
}
</pre>
<p>Then using the function to perform set intersection on the two sets to confirm our above results.</p>
<pre>
a &lt;- c(3, 5, 7, 11, 13, 20, 30)
b &lt;- c(3, 5, 13, 17, 7, 10)

set.intersection(a, b)
## [1]  3  5  7 13
</pre>
<h6>Subsets</h6>
<p>The concept of a subset of a set was introduced when we developed the set intersection operation. A set, <img alt="A" title="A" src="https://s0.wp.com/latex.php?latex=A&bg=ffffff&fg=000&s=0" class="latex">, is said to be a subset of <img alt="B" title="B" src="https://s0.wp.com/latex.php?latex=B&bg=ffffff&fg=000&s=0" class="latex">, written as <img alt="A \subset B" title="A \subset B" src="https://s0.wp.com/latex.php?latex=A+%5Csubset+B&bg=ffffff&fg=000&s=0" class="latex"> if all the elements of <img alt="A" title="A" src="https://s0.wp.com/latex.php?latex=A&bg=ffffff&fg=000&s=0" class="latex"> are also elements of <img alt="B" title="B" src="https://s0.wp.com/latex.php?latex=B&bg=ffffff&fg=000&s=0" class="latex">. Therefore, all sets are subsets of themselves and the empty set <img alt="\varnothing" title="\varnothing" src="https://s0.wp.com/latex.php?latex=%5Cvarnothing&bg=ffffff&fg=000&s=0" class="latex"> is a subset of every set.</p>
<p>We can write a simple function to test whether a set <img alt="a" title="a" src="https://s0.wp.com/latex.php?latex=a&bg=ffffff&fg=000&s=0" class="latex"> is a subset of <img alt="b" title="b" src="https://s0.wp.com/latex.php?latex=b&bg=ffffff&fg=000&s=0" class="latex">.</p>
<pre>
issubset &lt;- function(a, b) {
  for (i in 1:length(a)) {
    if (!(a[i] %in% b)) {
      return(FALSE)
    }
  }
  return(TRUE)
}
</pre>
<p>The union of two sets <img alt="a" title="a" src="https://s0.wp.com/latex.php?latex=a&bg=ffffff&fg=000&s=0" class="latex"> and <img alt="b" title="b" src="https://s0.wp.com/latex.php?latex=b&bg=ffffff&fg=000&s=0" class="latex"> has by definition subsets equal to <img alt="a" title="a" src="https://s0.wp.com/latex.php?latex=a&bg=ffffff&fg=000&s=0" class="latex"> and <img alt="b" title="b" src="https://s0.wp.com/latex.php?latex=b&bg=ffffff&fg=000&s=0" class="latex">, making a good test case for our function.</p>
<pre>
a &lt;- c(3, 5, 7, 11)
b &lt;- c(3, 5, 13, 17)

c &lt;- set.union(a, b)
c
## [1]  3  5  7 11 13 17
</pre>
<pre>
print(issubset(a, c))
## [1] TRUE
</pre>
<pre>
print(issubset(b, c))
## [1] TRUE
</pre>
<pre>
print(issubset(c(3, 5, 7, 4), a))
## [1] FALSE
</pre>
<h6>Summary</h6>
<p>This post introduced the common set operations unions and intersections and the axioms asserting those operations, as well as the definition of a subset of a set which arises naturally from the results of unions and intersections.</p>
<h6>References</h6>
<p>Axiom schema of specification. (2017, May 27). In Wikipedia, The Free Encyclopedia. From <a rel="nofollow" href="https://en.wikipedia.org/w/index.php?title=Axiom_schema_of_specification&oldid=782595557" target="_blank">https://en.wikipedia.org/w/index.php?title=Axiom_schema_of_specification&amp;oldid=782595557</a></p>
<p>Axiom of union. (2017, May 27). In Wikipedia, The Free Encyclopedia. From <a rel="nofollow" href="https://en.wikipedia.org/w/index.php?title=Axiom_of_union&oldid=782595523" target="_blank">https://en.wikipedia.org/w/index.php?title=Axiom_of_union&amp;oldid=782595523</a></p>
<p>Enderton, H. (1977). Elements of set theory (1st ed.). New York: Academic Press.</p>
<p>The post <a rel="nofollow" href="http://www.aaronschlegel.com/set-operations-unions-intersections-r/" target="_blank">Set Operations Unions and Intersections in R</a> appeared first on <a rel="nofollow" href="http://www.aaronschlegel.com/" target="_blank">Aaron Schlegel</a>.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.aaronschlegel.com/set-operations-unions-intersections-r/"> R – Aaron Schlegel</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/74OiFUTqMgg" height="1"></a></div>