R code for data processing