Completed R script