R code for exercise eight