<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/how-to-generate-a-secret-santa-list-with-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.revolutionanalytics.com/2017/11/how-to-generate-a-secret-santa-list-with-r.html"> Revolutions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p>Several recent blog posts have explored the <a rel="nofollow" href="https://en.wikipedia.org/wiki/Secret_Santa" target="_blank">Secret Santa</a> problem and provided solutions in R. This post provides a roundup of various solutions and how they are implemented in R. </p>
<p>If you wanted to set up a &quot;Secret Santa&quot; gift exchange at the office, you could put everyone's name into a hat and have each participant draw a name at random. The problem is that someone might draw their own name, but if that happens you can just reshuffle all the names back into the hat and start the process over. That's essentially what the R code below, from a <a rel="nofollow" href="http://selbydavid.com/2016/12/07/santa/" target="_blank">blog post by David Selby</a>, does:</p>
<blockquote class="twitter-tweet" data-lang="en">
<p dir="ltr" lang="it"><img alt="🙈" src="https://s.w.org/images/core/emoji/2.3/72x72/1f648.png" class="wp-smiley"><img alt="🎁" src="https://s.w.org/images/core/emoji/2.3/72x72/1f381.png" class="wp-smiley"> w/ code:<br>&quot;Secret Santa in R&quot; ✏ <a rel="nofollow" href="https://twitter.com/TeaStats?ref_src=twsrc%5Etfw" target="_blank">@TeaStats</a><a rel="nofollow" href="https://t.co/HFrh8w6CWU" target="_blank">https://t.co/HFrh8w6CWU</a> <a rel="nofollow" href="https://twitter.com/hashtag/rstats?src=hash&ref_src=twsrc%5Etfw" target="_blank">#rstats</a> <a rel="nofollow" href="https://t.co/cGfNA45CbG" target="_blank">pic.twitter.com/cGfNA45CbG</a></p>
<p>— Mara Averick (@dataandme) <a rel="nofollow" href="https://twitter.com/dataandme/status/934119945587298304?ref_src=twsrc%5Etfw" target="_blank">November 24, 2017</a></p></blockquote>
<p></p>
<p>That's not an entirely satisfying solution (at least to me), with all of the having to check for self-giving and restarting if so. <a rel="nofollow" href="http://notstatschat.tumblr.com/post/167885514256/secret-santa-collisions" target="_blank">Thomas Lumley calculates</a> that the chance of requiring a do-over is about 63% (for more than 2 participants, anyway), and on average you'd need about 2.7 tries to get a &quot;valid&quot; gift list. (This is an example of the <a rel="nofollow" href="https://en.wikipedia.org/wiki/Negative_binomial_distribution#Expectation" target="_blank">negative binomial</a> distribution in action: we keep on drawing a set of names from the hat, until we get a set that has no-one giving to themselves. You can generate 100 examples of this playing out in R with <code>rnbinom(100,1, exp(-1))+1</code> — the <code>+1</code> is for the final successful draw from the hat.)</p>
<p>An easier way might be simply to seat all the participants in random order in a circle and assign them to give a gift to the person on their right. This is easy to do in R, and doing it in code has the benefit of keeping the recipients secret from each other. First, let's select 10 names from the <a rel="nofollow" href="https://mran.microsoft.com/package/babynames" target="_blank">babynames</a> dataset:</p>
<div>
<pre><span>></span> library(babynames)
<span>></span> santas <span>&lt;-</span> sample(babynames<span>$</span>name, <span>10</span>, prob<span>=</span>babynames<span>$</span>prop) </pre>
</div>
<p>(Using <code>prob=babynames$prop</code> selects names according to their prevalence in US births 1880-2015.) Then, it's a simple matter of reordering the names at random, and assigning a gift to the next in line:</p>
<div>
<pre><span>></span> p <span>&lt;-</span> sample(santas)
<span>></span> cbind(santa<span>=</span>p, recipient<span>=</span>c(tail(p,<span>-1</span>),p[<span>1</span>]))
      santa       recipient  
 [<span>1</span>,] <span>&quot;Sherman&quot;</span>   <span>&quot;Shayna&quot;</span>   
 [<span>2</span>,] <span>&quot;Shayna&quot;</span>    <span>&quot;Elizabeth&quot;</span>
 [<span>3</span>,] <span>&quot;Elizabeth&quot;</span> <span>&quot;Mary&quot;</span>     
 [<span>4</span>,] <span>&quot;Mary&quot;</span>      <span>&quot;Kathleen&quot;</span> 
 [<span>5</span>,] <span>&quot;Kathleen&quot;</span>  <span>&quot;Russell&quot;</span>  
 [<span>6</span>,] <span>&quot;Russell&quot;</span>   <span>&quot;James&quot;</span>    
 [<span>7</span>,] <span>&quot;James&quot;</span>     <span>&quot;Arlene&quot;</span>   
 [<span>8</span>,] <span>&quot;Arlene&quot;</span>    <span>&quot;Ruth&quot;</span>     
 [<span>9</span>,] <span>&quot;Ruth&quot;</span>      <span>&quot;Darryl&quot;</span>   
[<span>10</span>,] <span>&quot;Darryl&quot;</span>    <span>&quot;Sherman&quot;</span>  
</pre>
</div>
<p>Now, this &quot;sit in a circle&quot; process isn't <em>quite</em> the same as the &quot;keep on drawing names from the hat&quot; process. In the above example, the 10 names form a cycle, and it will never happen that Arlene gives to Ruth and Ruth gives to Arlene. Nonetheless, I think it's the simplest and fairest way of generating a Secret Santa list.</p>
<p>Thinking of the gift-giving relationship as a graph, the process above generates a <a rel="nofollow" href="https://en.wikipedia.org/wiki/Hamiltonian_path" target="_blank">Hamiltonian path</a> through each of the recipients, and never generates more than one clique. Tristan Mahr explores the <a rel="nofollow" href="https://tjmahr.github.io/secret-santa-graph-traversal/" target="_blank">graph-theory nature of the Secret Santa problem</a> in a blog post. There, he uses the <a rel="nofollow" href="https://mran.microsoft.com/package/DiagrammeR" target="_blank">DiagrammeR package</a> to solve variants of the Secret Santa problem by constructing graphs, which can created and visualized quite easily in R.</p>
<p><a rel="nofollow" href="https://tjmahr.github.io/secret-santa-graph-traversal/" target="_blank"><img alt="Santagraph" title="Santagraph" src="http://a4.typepad.com/6a0105360ba1c6970c01b7c9388c8c970b-400wi"></a></p>
<p>Finally, <a rel="nofollow" href="http://livefreeordichotomize.com/2017/11/15/secret-sampling/" target="_blank"><span>Sarah Lotspeich and Lucy D'Agostino McGowan</span></a> take the whole process one step further and show how to generate emails each participant using the <a rel="nofollow" href="https://github.com/ropenscilabs/ponyexpress" target="_blank">ponyexpress package</a>, notifying them of their Secret Santa recipient.</p>
</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://blog.revolutionanalytics.com/2017/11/how-to-generate-a-secret-santa-list-with-r.html"> Revolutions</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/qqwtXAgAxHY" height="1"></a></div>