This year at Mango we’re proudly sponsoring the Bath Cats & Dogs Home. To start our fundraising for them, we decided to run a sweepstake on the Grand National. We asked for £2 per horse, which would go to the cats and dogs home and the winner was promised a bottle of wine for their charitable efforts.
Working in a Data Science company I knew that I couldn’t simply pick names out of a hat for the sweepstake, ‘That’s not truly random!’ they would cry. So in my hour of need, I turned to our two university placement students Owen and Caroline to help me randomise the names in R.
Non-starter
To use an appropriate horse-based metaphor, I would class myself as a ‘non-starter’ in R – I’m not even near the actual race! My knowledge is practically non-existent (‘Do you just type alot of random letters?’) and up until this blog I didn’t even have RStudio on my laptop.
The first hurdle
We began by creating a list of the people who had entered the sweepstake. With some people betting on more than one horse their name was entered as many times as needed to correlate to how many bets they had laid down.
people_list <- c("Matt Glover", "Matt Glover", "Ed Gash", "Ed Gash", "Ed Gash", "Lisa S", "Toby", "Jen", "Jen", "Liz", "Liz", "Andrew M", "Nikki", "Chris James", "Yvi", "Yvi", "Yvi", "Beany", "Karina", "Chrissy", "Enrique", "Pete", "Karis", "Laura", "Ryan", "Ryan", "Ryan", "Ryan", "Ryan", "Owen", "Rich", "Rich", "Matt A", "Matt A", "Matt A", "Matt A", "Matt A", "Matt A", "Matt A", "Matt A")
I had now associated all the names with the object called people_list
. Next I created an object that contained numbers 1-40 to represent each horse.
horses_list <- 1:40
With the two sets of values ready to go, I wanted to display them in a table format to make it easier to match names and numbers.
assign_horses <- data.frame(Runners = horses_list, People = people_list) head(assign_horses) ## Runners People ## 1 1 Matt Glover ## 2 2 Matt Glover ## 3 3 Ed Gash ## 4 4 Ed Gash ## 5 5 Ed Gash ## 6 6 Lisa S
Now the data appeared in a table, but had not been randomised. To do this I used the sample
function to jumble up the people_list
names.
assign_horses <- data.frame(horses_list, sample(people_list))
Free Rein
Success! I had a list of numbers (1-40) representing the horses and a randomly jumbled up list of those taking part in the sweepstake.
At the time of writing (In RMarkdown!), unfortunately fate had randomly selected me the favourite to win. As you can imagine, this is something that will not make you popular in the office.
My First Trot
I hope you enjoyed my first attempt in R. I will definitely use it again to randomise our next sweepstake, though under intense supervision. I can still hear the cries of ‘FIX!’ around the office. It’s always an awkward moment when you win your own sweepstake…
Despite the controversy, it was fun to try out R in an accessible way and it helped me understand some of the basic functions available. Perhaps I’ll sit in on the next LondonR workshop and learn some more!
If you’d like to find out more about the Bath Cats & Dogs Home please visit here.