<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/the-x-factors-where-0-means-1/"></div></div><div><a data-url="https://www.r-bloggers.com/the-x-factors-where-0-means-1/" data-counturl="https://www.r-bloggers.com/the-x-factors-where-0-means-1/" data-count="horizontal" data-text="The X-Factors: Where 0 means 1" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ekonometrics.blogspot.com/2016/09/the-x-factors-where-0-means-1_33.html"> eKonometrics</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div dir="ltr">Hadley Wickham in <a target="_blank" href="https://www.r-bloggers.com/forcats-0-1-0-%F0%9F%90%88%F0%9F%90%88%F0%9F%90%88%F0%9F%90%88/">a recent blog post</a> mentioned that “Factors have a bad rap in R because they often turn up when you don’t want them.” I believe Factors are an even bigger concern. They not only turn up where you don’t want them, but they also turn things around when you don’t want them to.
<p>Consider the following example where I present a data set with two variables: <b><span>x </span></b>and <b><span>y</span></b>. I represent age in years as ‘<b><span>y</span></b>‘ and gender as a binary (0/1) variable as ‘<b><span>x</span></b>‘ where 1 represents males.</p>
<div><a target="_blank" href="https://i2.wp.com/3.bp.blogspot.com/-N_BpNXTzZl4/V8iQ4LCXdQI/AAAAAAAADow/dCaGwnB9uXYYyneG-7h3Q2gtTSyV6XWEgCLcB/s1600/Fig.1.PNG?ssl=1"><img src="https://i2.wp.com/3.bp.blogspot.com/-N_BpNXTzZl4/V8iQ4LCXdQI/AAAAAAAADow/dCaGwnB9uXYYyneG-7h3Q2gtTSyV6XWEgCLcB/s1600/Fig.1.PNG?w=456&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p>I compute the means for the two variables as follows:
<div></div>
<div>
<div>
<div><a target="_blank" href="https://i2.wp.com/3.bp.blogspot.com/-8_rlZQrVAF0/V8iRPvFrOPI/AAAAAAAADo0/V8bmo61Lla40cPQRfXCF8hX-kbDJWhsqwCLcB/s1600/Fig.2.PNG?ssl=1"><img src="https://i2.wp.com/3.bp.blogspot.com/-8_rlZQrVAF0/V8iRPvFrOPI/AAAAAAAADo0/V8bmo61Lla40cPQRfXCF8hX-kbDJWhsqwCLcB/s1600/Fig.2.PNG?w=456&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p></div>
<div>The average age is 43.6 years, and 0.454 suggests that 45.4% of the sample comprises males. So far so good. </div>
<div></div>
<div>Now let’s see what happens when I convert <b><span>x</span></b> into a factor variable using the following syntax:
<div><a target="_blank" href="https://i0.wp.com/3.bp.blogspot.com/-fThICBm_Xbo/V8iRh8QW2BI/AAAAAAAADo4/f5EsHKDZAL8aXfxRP7t5OWiR3a3feToNwCLcB/s1600/fig3.PNG?ssl=1"><img src="https://i0.wp.com/3.bp.blogspot.com/-fThICBm_Xbo/V8iRh8QW2BI/AAAAAAAADo4/f5EsHKDZAL8aXfxRP7t5OWiR3a3feToNwCLcB/s1600/fig3.PNG?w=456&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p>The above code adds a new variable <b><span>male </span></b>to the data set, and assigns labels <b>female </b>and <b>male </b>to the categories 0 and 1 respectively.</div>
<div>
<div></div>
<div>I compute the average age for males and females as follows:</div>
<div></div>
<div>
<div>
<div><a target="_blank" href="https://i1.wp.com/1.bp.blogspot.com/-fFO301jRHTI/V8iR5JJTyCI/AAAAAAAADo8/bHnv_nLiSYcgahfNsCPMoBRpd1qhxsmjQCLcB/s1600/fig4.PNG?ssl=1"><img src="https://i1.wp.com/1.bp.blogspot.com/-fFO301jRHTI/V8iR5JJTyCI/AAAAAAAADo8/bHnv_nLiSYcgahfNsCPMoBRpd1qhxsmjQCLcB/s1600/fig4.PNG?w=456&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p></div>
</div>
<div>See what happens when I try to compute the mean for the variable ‘<span><b>male</b></span>‘.
</div>
<div>
<div><a target="_blank" href="https://i2.wp.com/3.bp.blogspot.com/-btOy9jst-xk/V8iSLxumbdI/AAAAAAAADpA/MCfbQUG4UI0XR8nuOfZvTX1hgC3RvAeKQCLcB/s1600/fig5.PNG?ssl=1"><img src="https://i2.wp.com/3.bp.blogspot.com/-btOy9jst-xk/V8iSLxumbdI/AAAAAAAADpA/MCfbQUG4UI0XR8nuOfZvTX1hgC3RvAeKQCLcB/s1600/fig5.PNG?w=456&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p></div>
<div>
<div>Once you factor a variable, you can’t compute statistics such as <b>mean </b>or <b>standard deviation</b>. To do so, you need to declare the factor variable as numeric. I create a new variable <span><b>gender</b> </span>that converts the <span><b>male</b> </span>variable to a numeric one.</div>
</div>
<div></div>
<div>
<div><a target="_blank" href="https://i2.wp.com/4.bp.blogspot.com/-tsrFzDII69c/V8iSpwmgWcI/AAAAAAAADpE/LKnFOIdztRsnpulrExHpMT7Rzsel5z4sQCLcB/s1600/fig6.PNG?ssl=1"><img src="https://i2.wp.com/4.bp.blogspot.com/-tsrFzDII69c/V8iSpwmgWcI/AAAAAAAADpE/LKnFOIdztRsnpulrExHpMT7Rzsel5z4sQCLcB/s1600/fig6.PNG?w=456&ssl=1" border="0" data-recalc-dims="1"></a></div>
</div>
<div>I recompute the means below. </div>
<div></div>
<div>
<div><a target="_blank" href="https://i0.wp.com/1.bp.blogspot.com/-rTzN7bgD0b0/V8iS8NL1_II/AAAAAAAADpM/5rG8ZKDDYakEftXTH4gzYXYL01RpnWGrgCLcB/s1600/fig7.PNG?ssl=1"><img src="https://i0.wp.com/1.bp.blogspot.com/-rTzN7bgD0b0/V8iS8NL1_II/AAAAAAAADpM/5rG8ZKDDYakEftXTH4gzYXYL01RpnWGrgCLcB/s1600/fig7.PNG?w=456&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p></div>
<div>Note that the average for <b>males </b>is 1.45 and not 0.45. Why? When we created the factor variable, it turned zeros into ones and ones into twos. Let’s look at the data set below:</div>
<div></div>
<div>
<div><a target="_blank" href="https://i0.wp.com/1.bp.blogspot.com/-7Y8bzmRNoU4/V8iTQaeD3AI/AAAAAAAADpQ/T6Bn1aMMc_g-_H6kYR5jKEyL4H2MwHKHACLcB/s1600/fig8.PNG?ssl=1"><img src="https://i0.wp.com/1.bp.blogspot.com/-7Y8bzmRNoU4/V8iTQaeD3AI/AAAAAAAADpQ/T6Bn1aMMc_g-_H6kYR5jKEyL4H2MwHKHACLcB/s1600/fig8.PNG?w=456&ssl=1" border="0" data-recalc-dims="1"></a></div>
</div>
</div>
<div></div>
<div>Several algorithms in R expect the factor variable to be of 0/1 form. If this condition is not satisfied, the command returns an error. For instance, when I try to estimate the logit model with <span><b>gender</b> </span>as the dependent variable and <b><span>y </span></b>as the explanatory variable, R generates the following error:</div>
<div><span><b><br></b></span></div>
<div>
<div>
<div><a target="_blank" href="https://i2.wp.com/4.bp.blogspot.com/-czhqpBEIyMg/V8iTg6c9RFI/AAAAAAAADpU/B4M7vpf4vks7SzdsYWYeWPwM5zoTm809QCLcB/s1600/fig9.PNG?ssl=1"><img src="https://i2.wp.com/4.bp.blogspot.com/-czhqpBEIyMg/V8iTg6c9RFI/AAAAAAAADpU/B4M7vpf4vks7SzdsYWYeWPwM5zoTm809QCLcB/s1600/fig9.PNG?w=456&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p>Factor or no factor, I would prefer my zeros to stay as zeros!</p></div>
</div>
<div>
<p></div>
</div>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/the-x-factors-where-0-means-1/"></div></div><div><a data-url="https://www.r-bloggers.com/the-x-factors-where-0-means-1/" data-counturl="https://www.r-bloggers.com/the-x-factors-where-0-means-1/" data-count="vertical" data-text="The X-Factors: Where 0 means 1" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<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/2016/09/the-x-factors-where-0-means-1_33.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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/uq6tfVwnV_Y"></a></div>