Part 1 | Code
In part one of this tutorial, you learned about what distance and similarity mean for data and how to measure it. Now, let’s see how we can implement distance measures in R. We’re going to look at the built-in dist() function and visualize similarities with a ggplot2 tile plot, also called a heatmap.
Implementation in R: the dist() function
The simplest way to do distance measures in R is the dist() function. It works with matrices as well as data frames and has options for a lot of the measures we’ve gotten to know in the last part.
dist(x, method = "euclidean", diag = FALSE, upper = FALSE, p = 2)
The crucial argument here is method. It has six options — actually more like four and a half, but you’ll see: