completed R code for assignment