preprocess data and write R data file