added rest of R code