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”

Sometimes it produces NAs. Just re run it several times to confirm.

LikeLike

Yes, I realized there were some problems so I made an updated version last year. You can check out the post here:

https://thepracticalr.wordpress.com/2016/12/07/secret-santa-picker-2-using-r/

LikeLike