The dist()
function in R computes the distance between the given rows of an input data matrix. It returns a distance matrix computed by any of the specified methods for measuring distance.
dist(vect, method = ” “, diag = TRUE or FALSE, upper = TRUE or FALSE)
The dist()
function takes the following parameter values:
vect
: This is a numeric matrix, data frame, or dist
object.method
: This is the method to be used for measuring the distance. The method can be "canberra"
, "binary"
, "euclidean"
, "maximum"
, "manhattan"
, or "minkowski"
. This parameter must take any of the listed methods.diag
: This takes a logical value (TRUE
or FALSE
), specifying if the diagonal of the output matrix should be returned by print.dist
or not.upper
: This takes a logical value (TRUE
or FALSE
) specifying if the upper triangle of the output distance matrix should be returned by print.dist
or not.The dist()
function returns an object of class dist
.
# Creating the vector obectsa = c(3, 1, 2)b = c(4, 0, 3)c = c(5, 4, 3)# Using the rbind() function to row bind the vectors into a single matrixmymatrix <- rbind(a, b, c)# Implementing the dist() function to calculate the manhattan distance between the rows of the matrixdist(mymatrix, method = "manhattan", diag=TRUE, upper=TRUE)