R script edited and tested