Amy Whitehead's Research

the ecological musings of a conservation biologist


3 Comments

Santa’s Little Helper – a function for randomly assigning Secret Santa presents

Christmas can be a stressful and expensive time of year, with many gifts to buy for many people. My family have tried to eliminate some of this stress by having a Secret Santa gifting system where we all buy one present for one member of the family on behalf of everybody else, with a cap of $50 per present.

In our family scheme, there are 3 rules:
1. You can’t give a gift to yourself,
2. You can’t give a gift to your partner (we figure that this will happen anyway),
3. You can’t give a gift to someone you gave one to last year.

Somewhere along the line, I’ve become the designated present-assigner. Now I could write names on pieces of paper and put them in a hat but 1) writing out 11 names is tedious, 2) I don’t have a suitable hat and 3) the rules above mean that there are quite often gifting conflicts arise that require the need for a redraw. So I’ve whipped up a little function in R that does the job. (Admittedly it probably would have been faster to use bits of paper but now I’m prepared for next year).

SantasLittleHelper <- function(myFrame,guestList,conflictCols = NULL){
  myTest <- TRUE
  nElves <- 0

  while (myTest == TRUE){
    myOut <- data.frame(gifter = myFrame[,guestList],
                        giftee = sample(myFrame[,guestList],
                                        replace = FALSE,
                                        size=nrow(myFrame))
                        )

    # check that guests haven't drawn themselves
    guestTest<- unlist(lapply(1:nrow(myOut),function(x) {
      myOut$giftee[x] == myFrame[x,guestList]
      }))

    # check for gifting conflicts
    if(!is.null(conflictCols)){

    conflictTest <- unlist(lapply(1:nrow(myOut),function(x) {
      grepl(myOut$giftee[x],myFrame[x,conflictCols])
      }))

    myTest <- any(c(guestTest,conflictTest[!is.na(conflictTest)]))

    } else{
      myTest <- any(guestTest)
    }

    # count the number of iterations needed to avoid conflicts
    nElves <- nElves + 1
  }

  message(paste(nElves,"elves were needed to generate the gift list"))
  return(myOut)
}

SantasLittleHelper takes three arguments: myFrame is a dataframe containing the list of guests to be assigned gifts; GuestList is the character name of the column containing the list of guests; and conflictCols is an optional character vector of column names that identify gifting conflicts that need to be avoided [1]. The function randomly assigns a giftee to each guest and then checks for conflicts. If conflicts exist, the gifts are reassigned and this continues using a while loop until a suitable solution is found. The function returns a dataframe containing detailing the gifter – giftee combinations, as well as outputting the number of elves (or iterations) needed to generate the list.

To run this for the 2016 Whitehead Family Christmas, I generated a dataframe guests where the column guest lists all of the people coming to Christmas, partner identifies everybody’s significant others and presents2013 lists the assigned giftees from 2013 (the last time we did the Secret Santa thing).

guest partner presents2013
John Fay Jo
Sue Simon Fay
Amy Hamish Sue
Jo Phil Hamish
Ashley Naomi Phil
Naomi Ashley John
Phil Jo Simon
Hamish Amy Naomi
Simon Sue Ashley
Fay John Amy
Zac NA NA

These data are fed into SantasLittleHelper and, bingo, the Whitehead Family Christmas is sorted for another year.

giftList <- SantasLittleHelper(myFrame = guests,
                               guestList = "guest",
                               conflictCols  = c("partner","presents2013")
                               )
27 elves were needed to generate the gift list
gifter giftee
John Zac
Sue Ashley
Amy Phil
Jo Amy
Ashley Fay
Naomi Sue
Phil John
Hamish Jo
Simon Naomi
Fay Simon
Zac Hamish

Hope everyone survives the silly season!

[Merry Christmas everyone!](http://www.r-bloggers.com/merry-christmas-5/)

[1] Technically you could use this feature to rig the outcome to avoid getting a present from Great Aunt Myrtle or Bob from Accounts by adding in an additional conflict column. Use your powers wisely or the ghosts of christmas past may come back to bite you!


Leave a comment

Goodbye Melbourne, hello (insert adventure here)

As I shut down my laptop for the last time and handed back my office keys a couple of months ago, it was hard to believe that two and a half years earlier, I had packed up my life in shaky Christchurch and moved to Melbourne to start a one year contract with the Quantitative & Applied Ecology group. Like most of the positions in my random career trajectory, I came to Melbourne by (happy) accident. I had applied for a position elsewhere in Australia and didn’t get it. But the guy who interviewed me mentioned this other guy in Melbourne who might have a position. We had a brief chat over the phone which essentially covered “Do you know how to use MaxEnt & Zonation?” & “When can you start?” to which I replied “Sure” & “Soon”.  A month later, I moved to Melbourne to start work with Brendan Wintle. The work has been interesting and varied, although largely concentrating on developing species distribution models to use in real-life conservation planning exercises aimed at informing Strategic Assessments in several regions of Australia. This has included lots of modelling, over a hundred meetings with stakeholders and lots and lots of squinting at maps and swearing at R code.  I also worked with some amazing colleagues, now amazing friends, and generally had a great time in Melbourne. Amazingly, in the two years or so since I started this blog, I have never written about the research I’ve been doing in Melbourne. The QAECO blog has a couple of posts about incorporating social values into conservation planning and some poetry that I wrote about a colleague but, at some point, I should probably remedy that.  But for now, here’s a story about my next adventure….
Not long after I bid my fond farewell to the folks of QAECO, I flew west to Perth to be reunited with my partner of seven years. It had been 4.5 years since we had lived in the same city, so it seemed like it was probably time we did something about that. Since then, I’ve been enjoying the Perth sunshine, finishing up a few work projects and playing travel agent. For we are off on a bike ride.
A big bike ride. Approximately 4,600 km of backcountry bikepacking along the Rocky Mountains from Jasper in Canada to Antelope Wells on the Mexican border. It’s going to be a great adventure and utterly terrifying all at the same time.
The Great Divide
What happens when we get back in November is totally up in the air. While a (tiny) part of me feels that I should be worried about not having a plan (particularly an academic plan) for when we get back. But it is actually quite liberating to just have the open road and endless possibilities ahead.

Needless to say, not much will be happening in this little corner of the web in the next few months but you’ll be able to follow our progress (and tales of bear encounters and other fun stories) at ridingtherockyroad.wordpress.com


1 Comment

Nigel Latta (& me) in Antarctica (on the telly)

Last season, when I was in Antarctica, we had a visit from Nigel Latta, a criminal psychologist turned comedic documentary-maker who makes humorous shows about serious subjects. He was sent to Scott Base, Antarctica to live among the scientists and to discover what life on the Ice is really like and whether it holds the key to our futures. The first episode airs on March 4th at 8:30pm on TV One (in NZ only) and promises to be an entertaining ride.

Filming with Nigel and his team was one of the more entertaining media experiences I’ve had – we defused bombs in a tent, counted (& recounted & recounted & recounted & ….) a constantly moving mass of penguin chicks by trying to name them all, discussed the parenting abilities of penguins and their monogamous (or lack thereof) relationships. I watched (with delight) as Nigel got shat on by a South Polar skua and attacked by angry penguins, we restaged Shackelton’s famous dinner photo and generally had a rollocking good time. I look forward to seeing his thoughts of life On Thin Ice*.

If you miss it (& happen to live in NZ), you should be able to watch it OnDemand.

* Not that I will actually be able to watch it, what with not being in NZ and the restrictions of TV OnDemand….but I’d love to know what you think of it.