<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/introduction-to-set-theory-and-sets-with-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.aaronschlegel.com/introduction-set-theory-r/"> R – Aaron Schlegel</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Sets define a ‘collection’ of objects, or things typically referred to as ‘elements’ or ‘members.’ The concept of sets arises naturally when dealing with any collection of objects, whether it be a group of numbers or anything else. Conceptually, the following examples can be defined as a ‘set’:</p>
<ul>
<li>{1, 2, 3, 4}</li>
<li>{Red, Green, Blue}</li>
<li>{Cat, Dog}</li>
</ul>
<p>The first example is the set of the first four natural numbers. The second defines a set of the primary colors while the third example denotes a set of common household pets.</p>
<p>Since its development beginning in the 1870s with Georg Cantor and Richard Dedekind, set theory has become a foundational system of mathematics, and therefore its concepts constantly arise in the study of mathematics and is also an area of active research today.</p>
<p>This post will introduce some of the basic concepts of set theory, specifically the Zermelo-Fraenkel axiomatic system (more on that later), with R code to demonstrate these concepts.</p>
<h6>Set Notation</h6>
<p>Sets can be defined with lowercase, uppercase, script or Greek letters (in addition to subscripts and the like). Using several types of letters helps when dealing with hierarchies. Before diving into set theory, it’s best to define the common notation employed. One benefit of set theory being ubiquitousness in mathematics is learning its notation also helps in the understanding of other mathematical concepts.</p>
<ul>
<li><span data-display="false" class="wp-katex-eq">\forall x</span> – for every set <span data-display="false" class="wp-katex-eq">x</span></li>
<li><span data-display="false" class="wp-katex-eq">\exists x</span> – there exists such a set <span data-display="false" class="wp-katex-eq">x</span> that</li>
<li><span data-display="false" class="wp-katex-eq">\neg</span> – not</li>
<li><span data-display="false" class="wp-katex-eq">\wedge</span> – and</li>
<li><span data-display="false" class="wp-katex-eq">\vee</span> – or (one or the other or both)</li>
<li><span data-display="false" class="wp-katex-eq">\Rightarrow</span> – implies</li>
<li><span data-display="false" class="wp-katex-eq">\Leftrightarrow</span> – iff, if and only if.</li>
<li><span data-display="false" class="wp-katex-eq">A \cup B</span> – union of sets <span data-display="false" class="wp-katex-eq">A</span> and <span data-display="false" class="wp-katex-eq">B</span></li>
<li><span data-display="false" class="wp-katex-eq">A \cap B</span> – intersection of sets <span data-display="false" class="wp-katex-eq">A</span> and <span data-display="false" class="wp-katex-eq">B</span></li>
<li><span data-display="false" class="wp-katex-eq">x \in A</span> – x is an element of a set <span data-display="false" class="wp-katex-eq">A</span></li>
<li><span data-display="false" class="wp-katex-eq">x \notin A</span> – x is not an element of a set <span data-display="false" class="wp-katex-eq">A</span></li>
</ul>
<p>The <span data-display="false" class="wp-katex-eq">\Rightarrow</span> notation for implies can be thought of like an if statement in that it denotes the relation ‘if a then b.’</p>
<h6>Set Membership</h6>
<p>Set membership is written similar to:</p>
<p><span data-display="true" class="wp-katex-eq"> x \in A </span></p>
<p>Which can be stated as ‘<span data-display="false" class="wp-katex-eq">x</span> is an element of the set <span data-display="false" class="wp-katex-eq">A</span>.’ If <span data-display="false" class="wp-katex-eq">x</span> is not a member of <span data-display="false" class="wp-katex-eq">A</span>, we write:</p>
<p><span data-display="true" class="wp-katex-eq"> x \notin A </span></p>
<p>The symbol <span data-display="false" class="wp-katex-eq">\in</span> to denote set membership originated with Giuseppe Peano (Enderton, pp. 26).</p>
<p>Which is read ‘<span data-display="false" class="wp-katex-eq">x</span> is not an element of the set <span data-display="false" class="wp-katex-eq">A</span>.’ We can write an R function to implement the concept of set membership.</p>
<pre>
iselement &lt;- function(x, A) {
  if (x %in% A) {
    return(TRUE)
  }
  return(FALSE)
}
</pre>
<p>Let’s use our simple function to test if there exists some members in the set, <span data-display="false" class="wp-katex-eq">A = \{3, 5, 7, 11\}</span>.</p>
<pre>
A &lt;- c(3, 5, 7, 11)
eles &lt;- c(3, 5, 9, 10, (5 + 6))
for (i in 1:length(eles)) {
  print(iselement(i, A))
}
## [1] FALSE
## [1] FALSE
## [1] TRUE
## [1] FALSE
## [1] TRUE
</pre>
<p>Set membership leads into one of the first axioms of set theory under the Zermelo-Fraenkel system, the Principle of Extensionality.</p>
<h6>Principle of Extensionality</h6>
<p>The principle of extensionality states if two sets have the same members, they are equal. The formal definition of the principle of extensionality can be stated more concisely using the notation given above:</p>
<p><span data-display="true" class="wp-katex-eq"> \forall A \forall B (\forall x (x \in A \Leftrightarrow x \in B) \Rightarrow A = B) </span></p>
<p>Stated less concisely but still using set notation:</p>
<p>If two sets <span data-display="false" class="wp-katex-eq">A</span> and <span data-display="false" class="wp-katex-eq">B</span> are such that for every element (member) <span data-display="false" class="wp-katex-eq">x</span>:</p>
<p><span data-display="true" class="wp-katex-eq"> x \in A \qquad iff \qquad x \in B </span> Then <span data-display="false" class="wp-katex-eq">A = B</span>.</p>
<p>We can express this axiom through an R function to test for set equality.</p>
<pre>
isequalset &lt;- function(a, b) {
  
  a &lt;- unique(a)
  b &lt;- unique(b)
  
  an &lt;- length(a)
  
  if (an > length(b)) {
    return(FALSE)
  }
  
  for (i in 1:an) {
    if (!(a[i]) %in% b) {
      return(FALSE)
    }
  }
  
  return(TRUE)
}
</pre>
<p>We can now put the principle of extensionality in action with our R function!</p>
<pre>
# original set A to compare
A &lt;- c(3, 5, 7, 11)

# define some sets to test for equality
B &lt;- c(5, 7, 11, 3)
C &lt;- c(3, 4, 6, 5)
D &lt;- c(3, 5, 7, 11, 13)
E &lt;- c(11, 7, 5, 3)
G &lt;- c(3, 5, 5, 7, 7, 11)

# collect sets into a list to iterate
sets &lt;- list(B, C, D, E, G)

# using the isequalset() function, print the results of the equality tests.
for (i in sets) {
  print(isequalset(i, A))
}
## [1] TRUE
## [1] FALSE
## [1] FALSE
## [1] TRUE
## [1] TRUE
</pre>
<h6>Empty Sets and Singletons</h6>
<p>So far we have only investigated sets with two or more members. The empty set, denoted <span data-display="false" class="wp-katex-eq">\varnothing</span>, is defined as a set containing no elements and occurs surprisingly frequently in set-theoretic operations despite is seemingly straightforward and simple nature.</p>
<p>The empty set axiom, states the existence of an empty set concisely:</p>
<p><span data-display="true" class="wp-katex-eq"> \exists B \forall x \qquad x \notin B </span> Which can also be stated as ‘there is a set having no members.’</p>
<p>A set <span data-display="false" class="wp-katex-eq">{\varnothing}</span> can be formed whose only member is <span data-display="false" class="wp-katex-eq">\varnothing</span>. It is important to note <span data-display="false" class="wp-katex-eq">{\varnothing} \neq \varnothing</span> because <span data-display="false" class="wp-katex-eq">\varnothing \in {\varnothing}</span> but <span data-display="false" class="wp-katex-eq">\varnothing \notin \varnothing</span>. One can conceptually think of <span data-display="false" class="wp-katex-eq">{\varnothing}</span> as a container with nothing in it.</p>
<p>A singleton is a set with exactly one element, denoted typically by <span data-display="false" class="wp-katex-eq">{a}</span>. A nonempty set is, therefore, a set with one or more element. Thus a singleton is also nonempty. We can define another quick function to test if a given set is empty, a singleton or a nonempty set.</p>
<pre>
typeofset &lt;- function(a) {
  if (length(a) == 0) {
    return('empty set')
  }
  
  else if (length(a) == 1) {
    return('singleton')
  }
  
  else if (length(a) > 1) {
    return('nonempty set')
  }
  
  else {
    stop('could not determine type of set')
  }
}
</pre>
<pre>
A &lt;- c()
B &lt;- c(0)
C &lt;- c(1, 2)
D &lt;- list(c())

set_types &lt;- list(A, B, C, D)

for (i in set_types) {
  print(typeofset(i))
}
## [1] &quot;empty set&quot;
## [1] &quot;singleton&quot;
## [1] &quot;nonempty set&quot;
## [1] &quot;singleton&quot;
</pre>
<p>Note <span data-display="false" class="wp-katex-eq">D</span> is defined as a singleton because the set contains one element, the empty set <span data-display="false" class="wp-katex-eq">\varnothing</span>.</p>
<h6>Summary</h6>
<p>This post introduced some of the basic concepts of axiomatic set theory using the Zermelo-Fraenkel axioms by exploring the idea of set, set membership and some particular cases of sets such as the empty set and singletons. Set notation that will be used throughout not just set-theoretic applications but throughout mathematics was also introduced.</p>
<h6>References</h6>
<p>Barile, Margherita. “Singleton Set.” From MathWorld–A Wolfram Web Resource, created by Eric W. Weisstein. <a rel="nofollow" href="http://mathworld.wolfram.com/SingletonSet.html" target="_blank">http://mathworld.wolfram.com/SingletonSet.html</a></p>
<p>Enderton, H. (1977). Elements of set theory (1st ed.). New York: Academic Press.</p>
<p>Weisstein, Eric W. “Empty Set.” From MathWorld–A Wolfram Web Resource. <a rel="nofollow" href="http://mathworld.wolfram.com/EmptySet.html" target="_blank">http://mathworld.wolfram.com/EmptySet.html</a></p>
<p>Weisstein, Eric W. “Nonempty Set.” From MathWorld–A Wolfram Web Resource. <a rel="nofollow" href="http://mathworld.wolfram.com/NonemptySet.html" target="_blank">http://mathworld.wolfram.com/NonemptySet.html</a></p>
<p>The post <a rel="nofollow" href="http://www.aaronschlegel.com/introduction-set-theory-r/" target="_blank">Introduction to Set Theory and Sets 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/introduction-set-theory-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/mPIAAg10Czs" height="1"></a></div>