R script for calculating Distance matrices