<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/keith-matthews-exam-question/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://rmnppt.com/2016/12/15/matthews-scores.html"> Adventures in Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<blockquote>
<p>Help? I set an exam question with 10 boxes to be filled from 13 possible answers. If randomly completed, what should be the average mark?<br>
– @KeithRMatthews, 12:13 PM – 15 Dec 2016</p>
</blockquote>
<div>
<pre><code><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>answers</span><span> </span><span>&lt;-</span><span> </span><span>letters</span><span>[</span><span>1</span><span>:</span><span>10</span><span>]</span><span>
</span><span>options</span><span> </span><span>&lt;-</span><span> </span><span>letters</span><span>[</span><span>1</span><span>:</span><span>13</span><span>]</span><span>
</span></code></pre>
</div>
<p>functions to give random answers and mark them</p>
<div>
<pre><code><span>giveAnswers</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>options</span><span>)</span><span> </span><span>sample</span><span>(</span><span>options</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>replace</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span>markAnswers</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>correct</span><span>,</span><span> </span><span>given</span><span>)</span><span> </span><span>sum</span><span>(</span><span>given</span><span> </span><span>==</span><span> </span><span>correct</span><span>)</span><span>
</span></code></pre>
</div>
<p>collect scores</p>
<div>
<pre><code><span>scores</span><span> </span><span>&lt;-</span><span> </span><span>numeric</span><span>(</span><span>1e5L</span><span>)</span><span>
</span><span>for</span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>seq_along</span><span>(</span><span>scores</span><span>))</span><span> </span><span>{</span><span>
</span><span>scores</span><span>[</span><span>i</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>markAnswers</span><span>(</span><span>answers</span><span>,</span><span> </span><span>giveAnswers</span><span>(</span><span>options</span><span>))</span><span>
</span><span>}</span><span>
</span></code></pre>
</div>
<p>averages</p>
<div>
<pre><code><span>mean</span><span>(</span><span>scores</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] 0.766
</code></pre>
</div>
<div>
<pre><code><span>median</span><span>(</span><span>scores</span><span>)</span><span>
</span></code></pre>
</div>
<div>
<pre><code>## [1] 1
</code></pre>
</div>
<p>plot</p>
<div>
<pre><code><span>ggplot</span><span>(</span><span>data.frame</span><span>(</span><span>score</span><span> </span><span>=</span><span> </span><span>scores</span><span>),</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>scores</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_bar</span><span>(</span><span>stat</span><span> </span><span>=</span><span> </span><span>&quot;count&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="plot of chunk unnamed-chunk-5" src="https://i1.wp.com/rmnppt.com/figure/source/2016-12-15-matthews-scores/unnamed-chunk-5-1.png?w=456" data-recalc-dims="1"></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://rmnppt.com/2016/12/15/matthews-scores.html"> Adventures in Data</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/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/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/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/OU42LFzCheI"></a></div>