<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/a-question-and-an-answer-about-recoding-several-factors-simultaneously-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ekonometrics.blogspot.com/2018/10/a-question-about-recoding-several.html"> eKonometrics</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div dir="ltr">
<div><span lang="EN-US">Data manipulation is a breeze with amazing packages like </span><b><span lang="EN-US">plyr</span></b><span lang="EN-US"> and </span><b><span lang="EN-US">dplyr</span></b><span lang="EN-US">. Recoding factors, which could prove to be a daunting task especially for variables that have many categories, can easily be accomplished with these packages. However, it is important for those learning Data Science to understand how the basic R works.</span></div>
<div></div>
<div><span lang="EN-US">In this regard, I seek help from R specialists about recoding factors using the base R. My question is about why one notation in recoding factors works while the other doesn’t. I’m sure for R enthusiasts, the answer and solution are straightforward. So, here’s the question.</span></div>
<div></div>
<div><span lang="EN-US">In the following code, I generate a vector with five categories and 300 observations. I convert the vector to a factor and tabulate it.</span></div>
<div></div>
<div>
<div><a rel="nofollow" href="https://i2.wp.com/4.bp.blogspot.com/-raSFpPAsKo0/W7uNbtYmEsI/AAAAAAAAHcs/Fy2a2JLjesw0foiK1vlTsleghDshzbEsgCLcBGAs/s1600/Capture%2B1.PNG?ssl=1" target="_blank"><img border="0" data-original-height="188" src="https://i2.wp.com/4.bp.blogspot.com/-raSFpPAsKo0/W7uNbtYmEsI/AAAAAAAAHcs/Fy2a2JLjesw0foiK1vlTsleghDshzbEsgCLcBGAs/s1600/Capture%2B1.PNG?resize=450%2C188&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<p></div>
<div></div>
<div>Note that by using <b><span lang="EN-US">as.numeric</span></b> option, I could see the internal level structure for the respective character notation. Let’s say, I would like to recode categories <span lang="EN-US"><b>a</b> </span>and <b><span lang="EN-US">f </span></b>as missing. I can accomplish this with the following code.</div>
<div></div>
<div>
<div><a rel="nofollow" href="https://i0.wp.com/1.bp.blogspot.com/-TkQ2O3tdd38/W7uNhFWQP8I/AAAAAAAAHcw/o2LtMsQ1YoE0fAHiFk3NzhRNJtpsK8iKQCLcBGAs/s1600/Capture%2B2.PNG?ssl=1" target="_blank"><img border="0" data-original-height="83" src="https://i0.wp.com/1.bp.blogspot.com/-TkQ2O3tdd38/W7uNhFWQP8I/AAAAAAAAHcw/o2LtMsQ1YoE0fAHiFk3NzhRNJtpsK8iKQCLcBGAs/s1600/Capture%2B2.PNG?resize=355%2C83&ssl=1" data-recalc-dims="1" data-original-width="355"></a></div>
</div>
<div>Where 1 and 6 correspond to <b><span lang="EN-US">a </span></b>and <b><span lang="EN-US">f.</span></b></div>
<div></div>
<div>Note that I have used the position of the levels rather than the levels themselves to convert the values to missing. </div>
<div></div>
<div>So far so good.</div>
<div></div>
<div>Now let’s assume that I would like to convert categories <b><span lang="EN-US">a </span></b>and <b><span lang="EN-US">f </span></b>to <i>grades</i>. The following code, I thought, would work, but it didn’t. It returns varying and erroneous answers.</div>
<div>
<div><a rel="nofollow" href="https://i2.wp.com/1.bp.blogspot.com/-CW0Dho0hLUM/W7uNp-ZgtzI/AAAAAAAAHc0/HoDBDnYUJt0UKyJLFf8le03JBw7WscrgwCLcBGAs/s1600/Capture%2B3.PNG?ssl=1" target="_blank"><img border="0" data-original-height="157" src="https://i2.wp.com/1.bp.blogspot.com/-CW0Dho0hLUM/W7uNp-ZgtzI/AAAAAAAAHc0/HoDBDnYUJt0UKyJLFf8le03JBw7WscrgwCLcBGAs/s1600/Capture%2B3.PNG?resize=450%2C157&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<p>However, when I refer to levels explicitly, the script works as intended. See the script below.</p></div>
<div></div>
<div>
<div><a rel="nofollow" href="https://i0.wp.com/2.bp.blogspot.com/-iXLJYHwToAc/W7uNuTgYAgI/AAAAAAAAHc8/3MC6MHXmhEIT5BJEgJYt711fX7b_iNahwCLcBGAs/s1600/Capture%2B4.PNG?ssl=1" target="_blank"><img border="0" data-original-height="151" src="https://i0.wp.com/2.bp.blogspot.com/-iXLJYHwToAc/W7uNuTgYAgI/AAAAAAAAHc8/3MC6MHXmhEIT5BJEgJYt711fX7b_iNahwCLcBGAs/s1600/Capture%2B4.PNG?resize=450%2C151&ssl=1" data-recalc-dims="1" data-original-width="450"></a></div>
<p>Hence the question: Why one method works and the other doesn’t? Looking forward to responses from R experts.</p>
<h2>The Answer</h2>
<p>lebatsnok (https://stackoverflow.com/users/2787952/lebatsnok) answered the question on <a rel="nofollow" href="https://stackoverflow.com/questions/52695857/a-question-about-recoding-multiple-factor-levels-simultaneously-in-r" target="_blank">stackoverflow</a>. The solution is simple. The following code works:</p>
<div><a rel="nofollow" href="https://i1.wp.com/2.bp.blogspot.com/-26PFXS9FY_0/W7uhn4TO-VI/AAAAAAAAHdc/lGsF9DqEkRktbAOk5r-Upl8NGNPgcc2KgCLcBGAs/s1600/Capture%2B5.PNG?ssl=1" target="_blank"><img border="0" data-original-height="45" src="https://i1.wp.com/2.bp.blogspot.com/-26PFXS9FY_0/W7uhn4TO-VI/AAAAAAAAHdc/lGsF9DqEkRktbAOk5r-Upl8NGNPgcc2KgCLcBGAs/s1600/Capture%2B5.PNG?resize=245%2C45&ssl=1" data-recalc-dims="1" data-original-width="245"></a></div>
<p></p>
<div>
<div></div>
<div>The problem with my approach, as explained by lebastsnok, is the following:</div>
<div></div>
<blockquote><p>“levels(x) is a character vector with length 6, as.numeric(x) is a logical vector with length 300. So you’re trying to index a short vector with a much longer logical vector. In such an indexing, the index vector acts like a “switch”, TRUE indicating that you want to see an item in this position in the output, and FALSE indicating that you don’t. So which elements of levels(x) are you asking for? (This will be random, you can make it reproducible with set.seed if that matters.”</p></blockquote>
</div>
<div></div>
<div>
<div></div>
<div></div>
<div></div>
</div>
<p></div>
<div></div>
<p></p>
<div></div>
</div>
	<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://ekonometrics.blogspot.com/2018/10/a-question-about-recoding-several.html"> eKonometrics</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 alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/HnfMXxYtMwY" height="1"></a></div>