corrected R code