<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/cross-tabulation-with-xtabs-exercises/"></div></div><div><a data-url="http://www.r-bloggers.com/cross-tabulation-with-xtabs-exercises/" data-counturl="http://www.r-bloggers.com/cross-tabulation-with-xtabs-exercises/" data-count="horizontal" data-text="Cross Tabulation with Xtabs exercises" 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://r-exercises.com/2016/05/12/cross-tabulation-with-xtabs-exercises/"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" rel="attachment wp-att-697" href="http://i1.wp.com/r-exercises.com/wp-content/uploads/2016/05/xtabs-plot.png"><img sizes="(max-width: 300px) 100vw, 300px" srcset="http://r-exercises.com/wp-content/uploads/2016/05/xtabs-plot-150x150.png 150w, http://i2.wp.com/r-exercises.com/wp-content/uploads/2016/05/xtabs-plot-300x300.png?resize=300%2C300 300w, http://r-exercises.com/wp-content/uploads/2016/05/xtabs-plot.png 504w" alt="xtabs-plot" class="wp-image-697" src="http://i2.wp.com/r-exercises.com/wp-content/uploads/2016/05/xtabs-plot-300x300.png?resize=300%2C300" data-recalc-dims="1"></a>The <code>xtabs()</code> function creates contingency tables in frequency-weighted format. Use <code>xtabs()</code> when you want to numerically study the distribution of one categorical variable, or the relationship between two categorical variables. Categorical variables are also called “factor” variables in R.</p>
<p>Using a formula interface, <code>xtabs()</code> can create a contingency table, (also a “sparse matrix”), from cross-classifying factors, usually contained in a data frame.</p>
<p>Answers to the exercises are available <a target="_blank" href="http://r-exercises.com/2016/5/12/cross-tabulation-with-xtabs-solutions/">here</a>.</p>
<p><strong>Exercise 1</strong><br>
<code>xtabs()</code> with One Categorical Variable</p>
<p>Input the following required Data Frame:<br>
<code><br>
Data1 &lt;- data.frame(Reference = c(&quot;KRXH&quot;, &quot;KRPT&quot;, &quot;FHRA&quot;, &quot;CZKK&quot;, &quot;CQTN&quot;, &quot;PZXW&quot;, &quot;SZRZ&quot;, &quot;RMZE&quot;, &quot;STNX&quot;, &quot;TMDW&quot;), Status = c(&quot;Accepted&quot;, &quot;Accepted&quot;, &quot;Rejected&quot;, &quot;Accepted&quot;, &quot;Rejected&quot;, &quot;Accepted&quot;, &quot;Rejected&quot;, &quot;Rejected&quot;, &quot;Accepted&quot;, &quot;Accepted&quot;), Gender = c(&quot;Female&quot;, &quot;Male&quot;, &quot;Male&quot;, &quot;Female&quot;, &quot;Female&quot;, &quot;Female&quot;, &quot;Male&quot;, &quot;Female&quot;, &quot;Female&quot;, &quot;Female&quot;), Test = c(&quot;Test1&quot;, &quot;Test1&quot;, &quot;Test2&quot;, &quot;Test3&quot;, &quot;Test1&quot;, &quot;Test4&quot;, &quot;Test4&quot;, &quot;Test2&quot;, &quot;Test3&quot;, &quot;Test1&quot;), NewOrFollowUp = c(&quot;New&quot;, &quot;New&quot;, &quot;New&quot;, &quot;New&quot;, &quot;New&quot;, &quot;Follow-up&quot;, &quot;New&quot;, &quot;New&quot;, &quot;New&quot;, &quot;New&quot;))<br>
</code></p>
<p>The <code>xtabs()</code> function can display the frequency, or count, of the levels of categorical variables. For the first exercise, use the <code>xtabs()</code> function to find the count of levels in the variable, “<code>Status</code>“, within the above dataframe, “<code>Data1</code>“.</p>
<p><strong>Exercise 2</strong><br>
Two Categorical Variables – Discoving relationships within a dataset</p>
<p>Next, using the <code>xtabs()</code> function, apply two variables from “<code>Data1</code>“, to create a table delineating the relationship between the “<code>Reference</code>” category, and the “<code>Status</code>” category.</p>
<p><strong>Exercise 3</strong><br>
Three Categorical Variables – Creating a Multi-Dimensional Table</p>
<p>Apply three variables from “<code>Data1</code>” to create a Multi-Dimensional Cross-Tabulation of “<code>Status</code>“, “<code>Gender</code>“, and “<code>Test</code>“.</p>
<p><strong>Exercise 4</strong><br>
Creating Two Dimensional Tables from Multi-Dimensional<br>
Cross-Tabulations</p>
<p>Enclose the <code>xtabs()</code> formula from Exercise 3 within the “<code>ftable()</code>” function, to display a Multi-Dimensional Cross-Tabulation in two dimensions.</p>
<p><strong>Exercise 5</strong><br>
Row Percentages</p>
<p>The R package “<code>tigerstats</code>” is required for the next two exercises.</p>
<p><code>if(!require(tigerstats)) {install.packages(&quot;tigerstats&quot;); require(tigerstats)}<br>
library(tigerstats)</code></p>
<p>1) Create an <code>xtabs()</code> formula that cross-tabulates “<code>Status</code>“, and “<code>Test</code>“.<br>
2) Enclose the <code>xtabs()</code> formula in the tigerstats function, “<code>rowPerc()</code>” to display row percentages for “<code>Status</code>” by “<code>Test</code>“.</p>
<p><strong>Exercise 6</strong><br>
Column Percentages</p>
<p>1) Create an <code>xtab()</code> formula that cross-tabulates “<code>Reference</code>“, and “<code>Status</code>“.<br>
2) Use “<code>colPerc()</code>” to display column percentages for “<code>Reference</code>” by “<code>Status</code>“.</p>
<p><strong>Exercise 7</strong><br>
Plotting Cross-Tabulations</p>
<p>Use the “<code>plot()</code>” function, and the “<code>xtabs()</code>” function to plot “<code>Status</code>” by “<code>Gender</code>“.</p>
<p><strong>Exercise 8</strong><br>
<code>xtabs()</code> – Explanatory and Response Variables</p>
<p>In order to examine whether the explanatory variable “<code>Gender</code>” affects the response variable “<code> Status</code>“, create a two factor <code>xtabs()</code> formula with the Response variable as the first condition, and the Explanatory variable as the second condition.</p>
<p><strong>Exercise 9</strong><br>
Using <code>cbind()</code> with <code>xtabs()</code></p>
<p>Using the “<code>cbind()</code>” function within an <code>xtabs()</code> formula can define the last two columns of a flat table of your dataset. The variable after ~ (tilde) will display as the row data. For example, <code>ftable(xtabs(cbind(variable1, variable2) ~ variable3, data=&quot; &quot;))</code>.</p>
<p>For this exercise, create a flat table with columns for “<code>Gender</code>” and “<code>Test</code>“. The row variables are “<code>Reference</code>“.</p>
<p><strong>Exercise 10</strong><br>
Testing Correlation with <code>xtabs()</code></p>
<p>When processed through the “<code>summary()</code>” function, an <code>xtabs()</code> formula can test for independence of variables. Therefore, use <code>summary()</code> and <code>xtabs()</code> to test for a “<code>Reference</code>” affecting “<code>Status</code>” correlation.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/cross-tabulation-with-xtabs-exercises/"></div></div><div><a data-url="http://www.r-bloggers.com/cross-tabulation-with-xtabs-exercises/" data-counturl="http://www.r-bloggers.com/cross-tabulation-with-xtabs-exercises/" data-count="vertical" data-text="Cross Tabulation with Xtabs exercises" 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://r-exercises.com/2016/05/12/cross-tabulation-with-xtabs-exercises/"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://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="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/ZpypyK6eP2k"></a></div>