<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-theory-arbitrary-union-and-intersection-operations-with-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.aaronschlegel.com/set-theory-arbitrary-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 3 of 3 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 <a rel="nofollow" href="http://wp.me/p4aZEo-5Ri" target="_blank">union and intersection set operations</a> were introduced in a previous post using 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">. These set operations can be generalized to accept any number of sets.</p>
<h6>Arbitrary Set Unions Operation</h6>
<p>Consider a set of infinitely many sets:</p>
<img alt=" A = \large{\{b_0, b_1, b_2, \cdots \} \large} " title=" A = \large{\{b_0, b_1, b_2, \cdots \} \large} " src="https://s0.wp.com/latex.php?latex=+A+%3D+%5Clarge%7B%5C%7Bb_0%2C+b_1%2C+b_2%2C+%5Ccdots+%5C%7D+%5Clarge%7D+&bg=ffffff&fg=000&s=0" class="latex">
<p>It would be very tedious and unnecessary to repeat the union statement repeatedly for any non-trivial amount of sets, for example, the first few unions would be written as:</p>
<img alt=" \large{b_0 \cup b_1 \cup b_2 \cup b_3 \cup b_4 \cup b_5\large} " title=" \large{b_0 \cup b_1 \cup b_2 \cup b_3 \cup b_4 \cup b_5\large} " src="https://s0.wp.com/latex.php?latex=+%5Clarge%7Bb_0+%5Ccup+b_1+%5Ccup+b_2+%5Ccup+b_3+%5Ccup+b_4+%5Ccup+b_5%5Clarge%7D+&bg=ffffff&fg=000&s=0" class="latex">
<p>Thus a more general operation for performing unions is needed. This operation is denoted by the <img alt="\bigcup" title="\bigcup" src="https://s0.wp.com/latex.php?latex=%5Cbigcup&bg=ffffff&fg=000&s=0" class="latex"> symbol. For example, the set <img alt="A" title="A" src="https://s0.wp.com/latex.php?latex=A&bg=ffffff&fg=000&s=0" class="latex"> above and the desired unions of the member sets can be generalized to the following using the new notation:</p>
<img alt=" \large{\bigcup A = \bigcup_i b_i} " title=" \large{\bigcup A = \bigcup_i b_i} " src="https://s0.wp.com/latex.php?latex=+%5Clarge%7B%5Cbigcup+A+%3D+%5Cbigcup_i+b_i%7D+&bg=ffffff&fg=000&s=0" class="latex">
<p>We can then state the following definition: 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">, the union <img alt="\bigcup A" title="\bigcup A" src="https://s0.wp.com/latex.php?latex=%5Cbigcup+A&bg=ffffff&fg=000&s=0" class="latex"> of <img alt="A" title="A" src="https://s0.wp.com/latex.php?latex=A&bg=ffffff&fg=000&s=0" class="latex"> is defined by:</p>
<img alt=" \large{\bigcup A = \{x \space | \space (\exists b \in A) \space x \in b \} \large} " title=" \large{\bigcup A = \{x \space | \space (\exists b \in A) \space x \in b \} \large} " src="https://s0.wp.com/latex.php?latex=+%5Clarge%7B%5Cbigcup+A+%3D+%5C%7Bx+%5Cspace+%7C+%5Cspace+%28%5Cexists+b+%5Cin+A%29+%5Cspace+x+%5Cin+b+%5C%7D+%5Clarge%7D+&bg=ffffff&fg=000&s=0" class="latex">
<p>For example, consider the three sets:</p>
<p><img alt=" \large{a = \{2, 4, 6 \} \qquad b = \{3, 5, 7\} \qquad c = \{2, 3, 8 \} \large}" title=" \large{a = \{2, 4, 6 \} \qquad b = \{3, 5, 7\} \qquad c = \{2, 3, 8 \} \large}" src="https://s0.wp.com/latex.php?latex=+%5Clarge%7Ba+%3D+%5C%7B2%2C+4%2C+6+%5C%7D+%5Cqquad+b+%3D+%5C%7B3%2C+5%2C+7%5C%7D+%5Cqquad+c+%3D+%5C%7B2%2C+3%2C+8+%5C%7D+%5Clarge%7D&bg=ffffff&fg=000&s=0" class="latex"> The union of the three sets is written as:<br>
<img alt=" \large{\bigcup \Big\{\{2,4,6\}, \{3,5,7\}, \{2,3,8\} \Big\} = \{2,3,4,5,6,7,8\}} " title=" \large{\bigcup \Big\{\{2,4,6\}, \{3,5,7\}, \{2,3,8\} \Big\} = \{2,3,4,5,6,7,8\}} " src="https://s0.wp.com/latex.php?latex=+%5Clarge%7B%5Cbigcup+%5CBig%5C%7B%5C%7B2%2C4%2C6%5C%7D%2C+%5C%7B3%2C5%2C7%5C%7D%2C+%5C%7B2%2C3%2C8%5C%7D+%5CBig%5C%7D+%3D+%5C%7B2%2C3%2C4%2C5%2C6%2C7%2C8%5C%7D%7D+&bg=ffffff&fg=000&s=0" class="latex"></p>
<p>Recalling our union axiom from a previous post, 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>As we are now dealing with an arbitrary amount of sets, we need an updated version of the union axiom to account for the change.</p>
<p>Restating the union axiom:</p>
<p>For any set <img alt="A" title="A" src="https://s0.wp.com/latex.php?latex=A&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"> whose members are the same elements of 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">. Stated more formally:</p>
<img alt=" \large{\forall x \big[ x \in B \space \Leftrightarrow \space (\exists b \in A) \space x \in b \big] }" title=" \large{\forall x \big[ x \in B \space \Leftrightarrow \space (\exists b \in A) \space x \in b \big] }" src="https://s0.wp.com/latex.php?latex=+%5Clarge%7B%5Cforall+x+%5Cbig%5B+x+%5Cin+B+%5Cspace+%5CLeftrightarrow+%5Cspace+%28%5Cexists+b+%5Cin+A%29+%5Cspace+x+%5Cin+b+%5Cbig%5D+%7D&bg=ffffff&fg=000&s=0" class="latex">
<p>The definition of <img alt="\bigcup A" title="\bigcup A" src="https://s0.wp.com/latex.php?latex=%5Cbigcup+A&bg=ffffff&fg=000&s=0" class="latex"> can be stated as:</p>
<img alt=" \large{x \in \bigcup A \Leftrightarrow (\exists b \in A) \space x \in b} " title=" \large{x \in \bigcup A \Leftrightarrow (\exists b \in A) \space x \in b} " src="https://s0.wp.com/latex.php?latex=+%5Clarge%7Bx+%5Cin+%5Cbigcup+A+%5CLeftrightarrow+%28%5Cexists+b+%5Cin+A%29+%5Cspace+x+%5Cin+b%7D+&bg=ffffff&fg=000&s=0" class="latex">
<p>For example, we can demonstrate the updated axiom with the union of four sets <img alt="\{a, b, c, d\}" title="\{a, b, c, d\}" src="https://s0.wp.com/latex.php?latex=%5C%7Ba%2C+b%2C+c%2C+d%5C%7D&bg=ffffff&fg=000&s=0" class="latex">:</p>
<p><img alt=" \large{\bigcup \{a, b, c, d \} = \big\{(\exists B \in A) \space x \in \{a, b, c, d\}\big\} \large}" title=" \large{\bigcup \{a, b, c, d \} = \big\{(\exists B \in A) \space x \in \{a, b, c, d\}\big\} \large}" src="https://s0.wp.com/latex.php?latex=+%5Clarge%7B%5Cbigcup+%5C%7Ba%2C+b%2C+c%2C+d+%5C%7D+%3D+%5Cbig%5C%7B%28%5Cexists+B+%5Cin+A%29+%5Cspace+x+%5Cin+%5C%7Ba%2C+b%2C+c%2C+d%5C%7D%5Cbig%5C%7D+%5Clarge%7D&bg=ffffff&fg=000&s=0" class="latex"><br>
<img alt=" \large{ \bigcup \{a, b, c, d \} = a \cup b \cup c \cup d \large}" title=" \large{ \bigcup \{a, b, c, d \} = a \cup b \cup c \cup d \large}" src="https://s0.wp.com/latex.php?latex=+%5Clarge%7B+%5Cbigcup+%5C%7Ba%2C+b%2C+c%2C+d+%5C%7D+%3D+a+%5Ccup+b+%5Ccup+c+%5Ccup+d+%5Clarge%7D&bg=ffffff&fg=000&s=0" class="latex"></p>
<p>We can implement the set operation for an arbitrary amount of sets by expanding upon the function we wrote previously.</p>
<pre>
set.unions &lt;- function(a, b, ...) {
  u &lt;- a
  for (i in 1:length(b)) {
    if (!(b[i] %in% u)) {
      u &lt;- append(u, b[i])
    }
  }
  
  s &lt;- list(...)
  
  for (i in s) {
    for (j in i) {
      if (!(j %in% u)) {
        u &lt;- append(u, j)
      }
    }
  }
  return(u)
}
</pre>
<p>Perform the set union operation of four sets:</p>
<img alt=" \large{a = \{1,2,3\} \qquad b = \{3,4,5\} \qquad c = \{1,4,6\} \qquad d =\{2,5,7\} \large}" title=" \large{a = \{1,2,3\} \qquad b = \{3,4,5\} \qquad c = \{1,4,6\} \qquad d =\{2,5,7\} \large}" src="https://s0.wp.com/latex.php?latex=+%5Clarge%7Ba+%3D+%5C%7B1%2C2%2C3%5C%7D+%5Cqquad+b+%3D+%5C%7B3%2C4%2C5%5C%7D+%5Cqquad+c+%3D+%5C%7B1%2C4%2C6%5C%7D+%5Cqquad+d+%3D%5C%7B2%2C5%2C7%5C%7D+%5Clarge%7D&bg=ffffff&fg=000&s=0" class="latex">
<pre>
a &lt;- c(1,2,3)
b &lt;- c(3,4,5)
c &lt;- c(1,4,6)
d &lt;- c(2,5,7)

set.unions(a, b, c, d)
## [1] 1 2 3 4 5 6 7
</pre>
<h6>Intersections of an Arbitrary Number of Sets</h6>
<p>The intersection set operation can also be generalized to any number of sets. Consider the previous set containing an infinite number of sets.</p>
<img alt=" \large{A = \{b_0, b_1, b_2, \cdots \}} " title=" \large{A = \{b_0, b_1, b_2, \cdots \}} " src="https://s0.wp.com/latex.php?latex=+%5Clarge%7BA+%3D+%5C%7Bb_0%2C+b_1%2C+b_2%2C+%5Ccdots+%5C%7D%7D+&bg=ffffff&fg=000&s=0" class="latex">
<p>As before, writing out all the intersections would be tedious and not elegant. The intersection can instead be written as:</p>
<img alt=" \large{\bigcap A = \bigcap_i b_i} " title=" \large{\bigcap A = \bigcap_i b_i} " src="https://s0.wp.com/latex.php?latex=+%5Clarge%7B%5Cbigcap+A+%3D+%5Cbigcap_i+b_i%7D+&bg=ffffff&fg=000&s=0" class="latex">
<p>As before in our previous example of set intersections, there is no need for a separate axiom for intersections, unlike unions. Instead, we can state the following theorem, for a nonempty set <img alt="A" title="A" src="https://s0.wp.com/latex.php?latex=A&bg=ffffff&fg=000&s=0" class="latex">, a set <img alt="B" title="B" src="https://s0.wp.com/latex.php?latex=B&bg=ffffff&fg=000&s=0" class="latex"> exists that such for any element <img alt="x" title="x" src="https://s0.wp.com/latex.php?latex=x&bg=ffffff&fg=000&s=0" class="latex">:</p>
<img alt=" \large{x \in B \Leftrightarrow x \in \forall A} " title=" \large{x \in B \Leftrightarrow x \in \forall A} " src="https://s0.wp.com/latex.php?latex=+%5Clarge%7Bx+%5Cin+B+%5CLeftrightarrow+x+%5Cin+%5Cforall+A%7D+&bg=ffffff&fg=000&s=0" class="latex">
<p>Consider the following four sets:</p>
<img alt=" \large{a = \{1,2,3\} \qquad b = \{1,3,5\} \qquad c = \{1,4,5,3\} \qquad d = \{2,6,1,3\}} " title=" \large{a = \{1,2,3\} \qquad b = \{1,3,5\} \qquad c = \{1,4,5,3\} \qquad d = \{2,6,1,3\}} " src="https://s0.wp.com/latex.php?latex=+%5Clarge%7Ba+%3D+%5C%7B1%2C2%2C3%5C%7D+%5Cqquad+b+%3D+%5C%7B1%2C3%2C5%5C%7D+%5Cqquad+c+%3D+%5C%7B1%2C4%2C5%2C3%5C%7D+%5Cqquad+d+%3D+%5C%7B2%2C6%2C1%2C3%5C%7D%7D+&bg=ffffff&fg=000&s=0" class="latex">
<p>The intersection of the sets is written as:</p>
<p><img alt=" \large{\bigcap \big\{\{1,2,3,5\}, \{1,3,5\}, \{1,4,5,3\}, \{2,5,1,3\}\big\}} " title=" \large{\bigcap \big\{\{1,2,3,5\}, \{1,3,5\}, \{1,4,5,3\}, \{2,5,1,3\}\big\}} " src="https://s0.wp.com/latex.php?latex=+%5Clarge%7B%5Cbigcap+%5Cbig%5C%7B%5C%7B1%2C2%2C3%2C5%5C%7D%2C+%5C%7B1%2C3%2C5%5C%7D%2C+%5C%7B1%2C4%2C5%2C3%5C%7D%2C+%5C%7B2%2C5%2C1%2C3%5C%7D%5Cbig%5C%7D%7D+&bg=ffffff&fg=000&s=0" class="latex"> <img alt=" = \large{\{1,2,3,5\} \cap \{1,3,5\} \cap \{1,4,5,3\} \cap \{2,5,1,3\} = \{1,3,5\}} " title=" = \large{\{1,2,3,5\} \cap \{1,3,5\} \cap \{1,4,5,3\} \cap \{2,5,1,3\} = \{1,3,5\}} " src="https://s0.wp.com/latex.php?latex=+%3D+%5Clarge%7B%5C%7B1%2C2%2C3%2C5%5C%7D+%5Ccap+%5C%7B1%2C3%2C5%5C%7D+%5Ccap+%5C%7B1%2C4%2C5%2C3%5C%7D+%5Ccap+%5C%7B2%2C5%2C1%2C3%5C%7D+%3D+%5C%7B1%2C3%2C5%5C%7D%7D+&bg=ffffff&fg=000&s=0" class="latex"></p>
<p>We can write another function to implement the set intersection operation given any number of sets.</p>
<pre>
set.intersections &lt;- function(a, b, ...) {

  intersect &lt;- vector()
  for (i in a) {
    if (i %in% b) {
      intersect &lt;- append(intersect, i)
    }
  }
  
  s &lt;- list(...)
  
  for (i in s) {
    for (j in i) {
      if (j %in% intersect) {
        intersect &lt;- append(intersect, j)
      }
    }
  }
  intersect &lt;- unique(intersect)
  
  return(intersect)
}
</pre>
<p>Perform set intersections of the four sets specified earlier.</p>
<pre>
a &lt;- c(1,2,3,5)
b &lt;- c(1,3,5)
c &lt;- c(1,4,5,3)
d &lt;- c(2,5,1,3)

set.intersections(a, b, c, d)
## [1] 1 3 5
</pre>
<h6>References</h6>
<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-theory-arbitrary-unions-intersections-r/" target="_blank">Set Theory Arbitrary Union and Intersection Operations with 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-theory-arbitrary-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/V4vhNrZYcn8" height="1"></a></div>