added annotated R code