Secret Sampling

’Tis the season for white elephant / גמד וענק / Yankee swap / secret santa-ing! We thought it’d be particularly fun to do it #rstats style.
holiday cheer

Sarah Lotspeich and Lucy D’Agostino McGowan


November 15, 2017

’Tis the season for white elephant / Yankee swap / secret santa-ing! There are various rules for this, for our version:

🏷 each participant receives the name of someone else to purchase a gift for
🎁 gifts are exchanged at a party
🤔 the receiver is tasked with guessing who the gift-giver was!

We thought it’d be particularly fun to do it #rstats style.

Assigning partners

We had our interested classmates sign up on a Google Form, resulting in a Google Sheet with each participant’s name, email, and interests.

df <- gs_url("") %>%

We began by making a tibble that has name in the first column and assigned a random partner in the second.

dat <- tibble(
  name = df$name,
  partner = sample(df$name)

“But what if someone was assigned to be their own partner!” you may ask. Have no fear! A while loop is here! We allow a while loop to iterate until every individual is assigned a partner (who isn’t them!).

while (any(dat$name == dat$partner)) {
  dat <- tibble(
    name = df$name,
    partner = sample(df$name)

This is likely not the most efficient way, but we only had 23 friends so it’s 👌 – if you get excited about efficiency and would like to share a quicker way to do it, please do! We ❤️ to learn!

Edit: Looks like our 🙏 was answered!

This could look something like:

dat <- df %>%
  select(name) %>%
  mutate(name = sample(name)) %>%
  mutate(partner = lag(name))
dat$partner[1] <- dat$name[nrow(dat)]

Or like this! (Thanks to our #livefreeordichtomize partner in crime + emoji friend Romain 👯)

We then do a join to pull the emails and interests back in.

df_name <- df[ , c("name", "email")]
df_partner <- df[ , c("name", "interests")]
dat <- dat %>%
  left_join(df_name, c("name" = "name")) %>%
  left_join(df_partner, c("partner" = "name"))

Sending the email

Karthik and I had a blast at the rOpenSci unconf creating the 🐴 ponyexpress package - what a delightful chance to use it! We didn’t want the secret to be spoiled (i.e. we didn’t want to know who would be buying us gifts!), so we wrote a script to send an email to our classmates.

## devtools::install_github("ropenscilabs/ponyexpress")

body <- "Dear {name},

You have been assigned to surprise <b>{partner}</b> with a happy gift! {partner} told us they have the following interests:


Remember, the price limit is $20. 

<img src = ''> </img>

Happy white elephant-ing,
Sarah & Lucy"

our_template <- glue::glue(glitter_template)

parcel <- parcel_create(dat,
                        sender_name = "Lucy",
                        sender_email = "",
                        subject = "Secret Santa!",
                        template = our_template)



This will create an email like this:

And that’s it! Happy gift-ing 🎁!