Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@adam-garcia
Created December 13, 2017 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adam-garcia/c566d8e85521c8ffb9b5780651fb67e8 to your computer and use it in GitHub Desktop.
Save adam-garcia/c566d8e85521c8ffb9b5780651fb67e8 to your computer and use it in GitHub Desktop.
#' santa(guests) is a function that assigns guests to a "secret santa" gift swap
#' @param guests the vector of partygoers
#' @return a tibble, with <guests> assigned in "to" and "from" columns
library(magrittr)
library(tibble)
santa <- function(guests) {
n <- length(guests)
tibble (
from = guests %>% sample(n),
to = c(from[-1], from[1])
) %>%
return()
}
santa(letters[1:10])
#> # A tibble: 10 x 2
#> from to
#> <chr> <chr>
#> 1 e h
#> 2 h b
#> 3 b d
#> 4 d a
#> 5 a g
#> 6 g c
#> 7 c i
#> 8 i j
#> 9 j f
#> 10 f e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment