R script that does the job