Completed R code for assignment 2 with detailed comments.