Completed R code.