Centroids and distances in R