Completed R code for Assignemnet 2