R code for Coursera class