The complete R code for assignment 2