# Secret Santa Picker using R

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

**The Practical R**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Here’s a quick post on making a secret santa picker using R. The code eliminates a person from picking themselves, otherwise it’s no frills.

#set the variable for the number of people npeople=5 fam=matrix(ncol=1, nrow=npeople, NA) fam[1,1]="name1" fam[2,1]="name2" fam[3,1]="name3" fam[4,1]="name4" fam[5,1]="name5" fam2=matrix(ncol=1, nrow=npeople, NA) names=c("name1","name2","name3","name4","name5") 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 (xx2[1]!=fam[i,1]){ fam2[i,1]=xx2[1] } else{ fam2[i,1]=xx2[2]} #set up the new matrix with one less name used=which(xx2==fam2[i]) xx2[used]="zzzzz" xx2=sort(xx2)[1:(npeople-i)] } #add "has" to the matrix has=matrix(ncol=1,nrow=npeople, "has") #build the final matrices final=cbind(fam,has,fam2) #the final results final

[,1] [,2] [,3]

[1,] “name1” “has” “name4”

[2,] “name2” “has” “name3”

[3,] “name3” “has” “name5”

[4,] “name4” “has” “name2”

[5,] “name5” “has” “name1”

To

**leave a comment**for the author, please follow the link and comment on their blog:**The Practical R**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.