Laura Swales, Marketing and Events Assistant
Another month, another sweepstake to raise money for the Bath Cats & Dogs home!
This time, we picked the Eurovision song contest as our sweepstake of choice. After enjoying my first experience of using R to randomise the names for the previous sweepstake I decided to give it another go, but with a few tweaks.
During my first attempt in R, issues arose when I had been (innocently!) allocated the favourite horse to win. I had no way to prove that the R code had made the selection, as my work was not reproducible.
So with the cries of “cheater!” and “fix!”” still ringing in my ears, we started by setting a
seed. This meant that if someone else was to replicate my code they would get the same results; therefore removing the dark smudge against my good name.
At random I selected the number 6 at which to set my seed.
I next compiled my lists of people and Eurovision countries and associated them with correlating objects.
Once I had the lists associated with objects, I followed the same steps as my previous attempt in R. I put both objects into data frames and then used the
sample function to jumble up the names.
Fate had delivered me Denmark, who were nowhere near the favourites at the point of selection. I sighed with relief knowing that I had no chance of winning again and that perhaps maybe now I could start to re-build my reputation as an honest co-worker…
Before I finished my latest foray into R, we decided to create a function for creating sweepstakes in R.
I was talked down from picking the name
SweepstakeizzleR and decided upon the slightly more sensible
I entered the desired workings of the function, which followed from the above work in R.
Once done, I could use my newly created function to complete the work I had done before but in a much timelier fashion.
My very first function worked! Using a function like
sweepR will allow me to reliably reproduce the procedures I need for whatever task I’m working on. In this case it has enabled me to create a successfully random sweepstake mix of names and entries.
With great relief Israel won Eurovision and I was very happy to hand over the prize to Amanda.
I really enjoyed learning a little more about R and how I can create functions to streamline my work. Hopefully another reason will come up for me to learn even more soon!
R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more…