Added the R code

Comments are missing, the function is working properly.