revised R file with my answer to assignment2
attempt 2