I have the following data frame structured in terms of 3 variables, i.e Location, Latitude, and Longitude within every single group. I would like to calculate the euclidean distance between all unique location combinations within each group. So for instance, based on the data frame below: the euclidean distance between - (A - London and A - Zurich) and (A - Zurich and A - New York) and (A - New York and A - London). And on a similar note (B - New York and B - London).
Then the average of all these unique distance pairs then needs to be calculated.
euc_dist <- function(x1, x2){
return(sqrt(sum((x1 - x2)^2)))
}
id Group Location Latitude Longitude
1 A London 1 2
2 A New York 3 4
3 A Zurich 5 6
4 B New York 7 8
5 B New York 9 10
6 B London 11 12
The output should look like:
id Group Average Euclidean distance
1 A xx
2 B xx
Thank you in advance!