<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/secret-santa-picker-2-using-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://thepracticalr.wordpress.com/2016/12/07/secret-santa-picker-2-using-r/"> The Practical R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Last year I made a blog post about a Secret Santa picker <a target="_blank" rel="nofollow" href="https://thepracticalr.wordpress.com/2015/12/12/secret-santa-picker-using-r/">HERE</a>, but to use it required quite a bit of messing around with the code. So this year I decided to improve the whole thing by making it a function rather than a script. The function take two inputs, a list of names and a number of names. The code for the function is listed first followed by the code for the script used to call the function.</p>
<p>Here’s the function. Nothing needs to be changed in this code for it to run properly.</p>
<pre><code># make a function 
secret_santa &lt;-function(npeople, names){
  
  # this 'flag' is used to determine if the
  # function stays in or out of the while function
  flag = &quot;bad&quot;
  
  # first list of names
  namelist1 = matrix(names, ncol = 1, nrow = npeople)
  fam = matrix(ncol = 1, nrow = npeople, NA)
  
  while (flag == &quot;bad&quot;){
    
    # names to choose from
    namelist2 = matrix(ncol = 1, nrow = npeople, NA)
    
    for (i in 1:npeople){
      #pick the first name
      if (i==1){
        xx2 = sample(names, (npeople-i+1), replace=FALSE)
      } else
        xx2 = sample(xx2, (npeople-i+1), replace=FALSE)
      
      if (i == npeople &amp; xx2[1]==namelist1[i,1]){
        flag = &quot;bad&quot;
        
      }else if(xx2[1]!= namelist1[i,1]){
        namelist2[i,1] = xx2[1]
        flag = &quot;good&quot;
      } else{
        namelist2[i,1] = xx2[2]
        flag = &quot;good&quot;
        }
      
      
      #set up the new matrix with one less name
      used = which(xx2==namelist2[i])
      xx2[used] = &quot;zzzzz&quot;
      xx2 = sort(xx2)[1:(npeople-i)]
    }
    
    #flag
    #add &quot;has&quot; to the matrix
    has = matrix(ncol=1, nrow = npeople, &quot;has&quot;)
    
    #build the final matrices
    final = cbind(namelist1, has, namelist2)	
    #the final results
    #final
    
    
  }
  final
}
</code></pre>
<p>Save this function as “secret-santa-function.R” and we’ll call it from our script. Okay, now let’s make our script.</p>
<pre><code># call the function from the script
source(&quot;secret-santa-function.R&quot;)

### Function input
### make a list of names
names = c(&quot;James&quot;,&quot;Nick&quot;,&quot;Emily&quot;,&quot;Natasha&quot;,&quot;Bob&quot;, &quot;Teddy&quot;)
n = length(names)

#call the function
output &lt;-secret_santa(n, names)
output </code></pre>
<p>The list of names is the only input needed. In the case above it’s ‘names = c(“James”,”Nick”,”Emily”,”Natasha”,”Bob”, “Teddy”)’. The other variable the function needs is the number of names, which is read automatically from the <strong>length</strong> function. That’s it, you’re done. Call the function from the script and you’ve got your names.</p>
<p><img sizes="(max-width: 253px) 100vw, 253px" alt="names" class="wp-image-728" src="https://i0.wp.com/thepracticalr.files.wordpress.com/2016/12/names.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thepracticalr.wordpress.com/694/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/thepracticalr.wordpress.com/694/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" 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="https://thepracticalr.wordpress.com/2016/12/07/secret-santa-picker-2-using-r/"> The Practical R</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/bJZKGD56O7g"></a></div>