R code for coursera assignment 2