R script to generate tidy dataset