R code for Coursera course