Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

##### 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.

### Soundcheck

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.

set.seed(6)

I next compiled my lists of people and Eurovision countries and associated them with correlating objects.

people_list <- c(
"Andy M",
"Laura",
"Rachel",
"Owen",
"Yvi",
"Karis",
"Toby",
"Jen",
"Matty G",
"Tatiana",
"Amanda",
"Chrissy",
"Lisa",
"Lisa",
"Ben",
"Ben",
"Robert",
"Toby",
"Matt A",
"Lynn",
"Ruth",
"Julian",
"Karina",
"Colin",
"Colin")
countries_list <- c(
"Albania",
"Australia",
"Austria",
"Bulgaria",
"Cyprus",
"Czech Rep",
"Denmark",
"Estonia",
"Finland",
"France",
"Germany",
"Hungary",
"Ireland",
"Israel",
"Italy",
"Lithuania",
"Moldova",
"Norway",
"Portugal",
"Serbia",
"Slovenia",
"Spain",
"Sweden",
"The Netherlands",
"Ukraine",
"United Kingdom"
)

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.

assign_countries <- data.frame(people = people_list,
countries = sample(countries_list))

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...

### Encore

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 sweepR.

I entered the desired workings of the function, which followed from the above work in R.

sweepR <- function(a, b, seed = 1234){
set.seed(seed)
data.frame(a, sample(b))
}


Once done, I could use my newly created function to complete the work I had done before but in a much timelier fashion.

sweepR(people_list, countries_list)

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.

### WinneR

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!