R script to calculate statistics