R script to extract datasets