completed R code