R code used for coursework