Skip to content

Instantly share code, notes, and snippets.

@arcaravaggi
Last active March 14, 2018 15:08
Show Gist options
  • Save arcaravaggi/b990ff3b9545a453761af5f2195b151c to your computer and use it in GitHub Desktop.
Save arcaravaggi/b990ff3b9545a453761af5f2195b151c to your computer and use it in GitHub Desktop.
Calculate Euclidean distance between groups of ecological point data
# Function to calculate Euclidean distance between n sets of Principal Components
# Ensure all data are in data.matrix format
#
# x = data matrix 1
# y = data matrix 2
# d = distance method (pdist = unequal sets; dist = equal sets)
# e.g.
# distance <- euDist(dat1, dat2, dist = "dist")
# mean(distance)
# sd(distance)
euDist <- function(x, y, d = "pdist"){
if(d == "pdist"){
df <- pdist(x, y, indices.A = NULL, indices.B = NULL)
}
else{ if(d == "dist"){
df <- dist(x, y, method = "euclidean")
}}
df <- unlist(data.frame(as.matrix(df)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment