updated R code with new data model