Completed R code for assignment.