Euclidean distance: \(D = \sqrt{\sum{(x_i - x_j) ^ 2}}\)
# calculating by hand Euclidean distance
sqrt(sum( (iris[1, 1:4] - iris[2, 1:4])^2 ))
[1] 0.5385165
distance <- dist(iris[, 1:4]) # using dist()
The function dist(x = <table>, method = <method>)
calculates the pairwise distance between all rows in <table>
using the distance metric specified by <method>
(default = “eulicean”). Basically, it does the manual calculation I did for all rows.
The output is a symmetric matrix, with entries the distance between rows.
as.matrix(distance)[1:3, 1:3]
1 2 3
1 0.0000000 0.5385165 0.509902
2 0.5385165 0.0000000 0.300000
3 0.5099020 0.3000000 0.000000
See how the entry as.matrix(distance)[1, 2]
(equal to as.matrix(distance)[2, 1]
, being symmetric) is the same as what I calculated by hand.