Updated version of the R script for Coursera project