Last active
March 14, 2018 15:08
-
-
Save arcaravaggi/b990ff3b9545a453761af5f2195b151c to your computer and use it in GitHub Desktop.
Calculate Euclidean distance between groups of ecological point data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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