final version of R script