<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/use-unique-instead-of-levels-to-find-the-possible-values-of-a-character-variable-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://chemicalstatistician.wordpress.com/2018/03/10/use-unique-instead-of-levels-to-find-the-possible-values-of-a-character-variable-in-r/"> R programming – The Chemical Statistician</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>When I first encountered R, I learned to use the <a rel="nofollow" href="https://stat.ethz.ch/R-manual/R-devel/library/base/html/levels.html" target="_blank">levels()</a> function to find the possible values of a categorical variable.  However, I recently noticed something very strange about this function.</p>
<p>Consider the built-in data set <a rel="nofollow" href="https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/iris.html" target="_blank">“iris”</a> and its character variable “Species”.  Here are the possible values of “Species”, as shown by the levels() function.</p>
<pre>> levels(iris$Species)

[1] &quot;setosa&quot; &quot;versicolor&quot; &quot;virginica&quot;</pre>
<p>Now, let’s remove all rows containing “setosa”.  I will use the <a rel="nofollow" href="https://stat.ethz.ch/R-manual/R-devel/library/base/html/table.html" target="_blank">table()</a> function to confirm that no rows contain “setosa”, and then I will apply the levels() function to “Species” again.</p>
<pre>> iris2 = subset(iris, Species != 'setosa')
> table(iris2$Species)

    setosa versicolor virginica 
         0         50        50 


> levels(iris2$Species)

[1] &quot;setosa&quot; &quot;versicolor&quot; &quot;virginica&quot;</pre>
<p><span></span></p>
<p>The new data set “iris2” does not have any rows containing “setosa” as a possible value of “Species”, yet the levels() function still shows “setosa” in its output.</p>
<p>According to the user <a rel="nofollow" href="https://stackoverflow.com/users/4752675/g5w" target="_blank">G5W</a> in <a rel="nofollow" href="https://stackoverflow.com/" target="_blank">Stack Overflow</a>, this is <a rel="nofollow" href="https://stackoverflow.com/questions/42856796/levels-of-factor-still-show-after-deleting-the-elements" target="_blank">a desirable behaviour for the levels() function</a>.  Here is my interpretation of the intent behind the creators of base R: The possible values of a character variable are fundamental attributes of that variable, which should not be altered because of changes in the data.</p>
<p>Obviously, this can cause a lot of confusion and produce wrong information, so here is my solution: From now on, I will use the <a rel="nofollow" href="https://stat.ethz.ch/R-manual/R-devel/library/base/html/unique.html" target="_blank">unique()</a> function to find the possible values of a character variable.  Here is the result.</p>
<pre>> unique(iris2$Species)

[1] versicolor virginica 
Levels: setosa versicolor virginica</pre>
<p> </p>
<p>This is the output that I expect; “setosa” does not appear in the resulting vector.  However, unique() stills hows the original levels, which include “setosa” – that’s a nice feature.</p>
<p> </p>
<p>I thank my colleagues Layne Newhouse, Jack Davis, and Dmity Shopin for their <a rel="nofollow" href="https://www.linkedin.com/feed/update/urn:li:activity:6361963012454260736" target="_blank">valuable discussion about this on LinkedIn</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="https://chemicalstatistician.wordpress.com/2018/03/10/use-unique-instead-of-levels-to-find-the-possible-values-of-a-character-variable-in-r/"> R programming – The Chemical Statistician</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/ZZkEYwmA_4k" height="1"></a></div>