Final Version R script