R version of preprocess script almost complete