<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/getting-into-the-rhythm-the-eurovision-sweepstake/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.mango-solutions.com/blog/getting-into-the-rhythm-the-eurovision-sweepstake"> Mango Solutions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p> <img sizes="100vw" src="https://i1.wp.com/www.mango-solutions.com/blog/images/1/4/0/8/d/1408d31161ffa1d7798960839153b2bc0cf30137-eurovision-blog-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h5><em> Laura Swales, Marketing and Events Assistant </em></h5>
<p>Another month, another sweepstake to raise money for the Bath Cats &amp; Dogs home!</p>
<p>This time, we picked the Eurovision song contest as our sweepstake of choice. After enjoying my first experience of using R to randomise the names for the previous <a rel="nofollow" href="https://www.mango-solutions.com/blog/saddling-up-getting-on-the-horse-for-the-first-time" target="_blank">sweepstake</a> I decided to give it another go, but with a few tweaks.</p>
<h3>Soundcheck</h3>
<p>During my first attempt in R, issues arose when I had been (innocently!) allocated the favourite horse to win. I had no way to prove that the R code had made the selection, as my work was not reproducible.</p>
<p>So with the cries of “cheater!” and “fix!”” still ringing in my ears, we started by setting a <code>seed</code>. This meant that if someone else was to replicate my code they would get the same results; therefore removing the dark smudge against my good name.</p>
<p>At random I selected the number 6 at which to set my seed.</p>
<pre><code>set.seed(6)</code></pre>
<p>I next compiled my lists of people and Eurovision countries and associated them with correlating objects.</p>
<pre><code>people_list &lt;- c(
    &quot;Andy M&quot;,
    &quot;Adam&quot;,
    &quot;Laura&quot;,
    &quot;Rachel&quot;,
    &quot;Owen&quot;,
    &quot;Yvi&quot;,
    &quot;Karis&quot;,
    &quot;Toby&quot;,
    &quot;Jen&quot;,
    &quot;Matty G&quot;,
    &quot;Tatiana&quot;,
    &quot;Amanda&quot;,
    &quot;Chrissy&quot;,
    &quot;Lisa&quot;,
    &quot;Lisa&quot;,
    &quot;Ben&quot;,
    &quot;Ben&quot;,
    &quot;Robert&quot;,
    &quot;Toby&quot;,
    &quot;Matt A&quot;,
    &quot;Lynn&quot;,
    &quot;Ruth&quot;,
    &quot;Julian&quot;,
    &quot;Karina&quot;,
    &quot;Colin&quot;,
    &quot;Colin&quot;)</code></pre>
<pre><code>countries_list &lt;- c(
    &quot;Albania&quot;,
    &quot;Australia&quot;,
    &quot;Austria&quot;,
    &quot;Bulgaria&quot;,
    &quot;Cyprus&quot;,
    &quot;Czech Rep&quot;,
    &quot;Denmark&quot;,
    &quot;Estonia&quot;,
    &quot;Finland&quot;,
    &quot;France&quot;,
    &quot;Germany&quot;,
    &quot;Hungary&quot;,
    &quot;Ireland&quot;,
    &quot;Israel&quot;,
    &quot;Italy&quot;,
    &quot;Lithuania&quot;,
    &quot;Moldova&quot;,
    &quot;Norway&quot;,
    &quot;Portugal&quot;,
    &quot;Serbia&quot;,
    &quot;Slovenia&quot;,
    &quot;Spain&quot;,
    &quot;Sweden&quot;,
    &quot;The Netherlands&quot;,
    &quot;Ukraine&quot;,
    &quot;United Kingdom&quot;
  )</code></pre>
<p>Once I had the lists associated with objects, I followed the same steps as my previous attempt in R. I put both objects into data frames and then used the <code>sample</code> function to jumble up the names.</p>
<pre><code>assign_countries &lt;- data.frame(people = people_list,
                               countries = sample(countries_list))</code></pre>
<p>Task complete!</p>
<p>Fate had delivered me Denmark, who were nowhere near the favourites at the point of selection. I sighed with relief knowing that I had no chance of winning again and that perhaps maybe now I could start to re-build my reputation as an honest co-worker…</p>
<h3>Encore</h3>
<p>Before I finished my latest foray into R, we decided to create a function for creating sweepstakes in R.</p>
<p>I was talked down from picking the name <code>SweepstakeizzleR</code> and decided upon the slightly more sensible <code>sweepR</code>. </p>
<p>I entered the desired workings of the function, which followed from the above work in R.</p>
<pre><code>sweepR &lt;- function(a, b, seed = 1234){
 set.seed(seed)
 data.frame(a, sample(b))
}
</code></pre>
<p>Once done, I could use my newly created function to complete the work I had done before but in a much timelier fashion.</p>
<pre><code>sweepR(people_list, countries_list)</code></pre>
<p>My very first function worked! Using a function like <code>sweepR</code> will allow me to reliably reproduce the procedures I need for whatever task I’m working on. In this case it has enabled me to create a successfully random sweepstake mix of names and entries.</p>
<h3>WinneR</h3>
<p>With great relief Israel won Eurovision and I was very happy to hand over the prize to Amanda.</p>
<p>I really enjoyed learning a little more about R and how I can create functions to streamline my work. Hopefully another reason will come up for me to learn even more soon!</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://www.mango-solutions.com/blog/getting-into-the-rhythm-the-eurovision-sweepstake"> Mango Solutions</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/10n2X2Yej0s" height="1"></a></div>