{"id":1608,"date":"2017-12-01T16:43:35","date_gmt":"2017-12-01T23:43:35","guid":{"rendered":"https:\/\/blogs.ubc.ca\/aitkenlab\/?p=1608"},"modified":"2018-01-28T11:50:01","modified_gmt":"2018-01-28T18:50:01","slug":"are-you-playing-secret-santa-a-r-function","status":"publish","type":"post","link":"https:\/\/blogs.ubc.ca\/aitkenlab\/2017\/12\/01\/are-you-playing-secret-santa-a-r-function\/","title":{"rendered":"Are you playing Secret Santa? a R function"},"content":{"rendered":"<p>We&#8217;re playing Secret Santa in my family: you are giving a present to only one other person, and their identity is only known to you. You need to give them a handmade present or a present bought in a second-hand store. Well that last part is our own variation of the concept.<\/p>\n<p>You might be playing Secret Santa in your lab.<\/p>\n<p>Here is a little R function to sort the draw without anyone seeing the outcome, and without the need to involve an external participant. The only inputs are:<\/p>\n<p>-a vector of names of participants; for example c(&#8220;Patrick&#8221;,&#8221;Nicole&#8221;,&#8221;Maria&#8221;,&#8221;Pablo&#8221;)<\/p>\n<p>-the folder where you want the output files; for example &#8220;C:\/Users\/Maria\/SecretSanta&#8221;<\/p>\n<p>After you&#8217;ve run the function, send to each person the output file named after them.<\/p>\n<p>The function:<\/p>\n<pre>secretsanta=function(names,directory){\r\nchosen=names\r\nfor (i in 1:length(names)){\r\n if (names[i]%in% chosen){\r\n choice=sample(chosen[-which(chosen==names[i])],1)\r\n }else{\r\n choice=sample(chosen,1)\r\n }\r\n chosen=chosen[-which(chosen==choice)]\r\n write.table(paste(directory,\"\/\",names[i],\", you will give a present to \",choice,sep=\"\"),\r\n file=paste(names[i],\".txt\",sep=\"\"),quote=F,row.names=F,col.names=F)\r\n }\r\n return(\"Done! the files are in the provided directory. Merry Christmas.\")\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Load the function into R by entering the code above, and then run the function with your input:<\/p>\n<pre>secretsanta(names=c(\"Patrick\",\"Nicole\",\"Maria\",\"Pablo\"), directory=\"C:\/Users\/Maria\/SecretSanta\")<\/pre>\n<p>&nbsp;<\/p>\n<p>Merry Christmas!<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;re playing Secret Santa in my family: you are giving a present to only one other person, and their identity is only known to you. You need to give them a handmade present or a present bought in a second-hand store. Well that last part is our own variation of the concept. You might be [&hellip;]<\/p>\n","protected":false},"author":16511,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[369483],"tags":[7967],"class_list":["post-1608","post","type-post","status-publish","format-standard","hentry","category-techniques","tag-r"],"_links":{"self":[{"href":"https:\/\/blogs.ubc.ca\/aitkenlab\/wp-json\/wp\/v2\/posts\/1608","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.ubc.ca\/aitkenlab\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.ubc.ca\/aitkenlab\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/aitkenlab\/wp-json\/wp\/v2\/users\/16511"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/aitkenlab\/wp-json\/wp\/v2\/comments?post=1608"}],"version-history":[{"count":1,"href":"https:\/\/blogs.ubc.ca\/aitkenlab\/wp-json\/wp\/v2\/posts\/1608\/revisions"}],"predecessor-version":[{"id":1609,"href":"https:\/\/blogs.ubc.ca\/aitkenlab\/wp-json\/wp\/v2\/posts\/1608\/revisions\/1609"}],"wp:attachment":[{"href":"https:\/\/blogs.ubc.ca\/aitkenlab\/wp-json\/wp\/v2\/media?parent=1608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.ubc.ca\/aitkenlab\/wp-json\/wp\/v2\/categories?post=1608"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.ubc.ca\/aitkenlab\/wp-json\/wp\/v2\/tags?post=1608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}