R script to obtain "finaldata.txt"