Binary distance
The Binary distance between two vectors, A and B, is calculated as the proportion of elements that the two vectors share.
Here,
- vect1 is the first vector
- vect2 is the second vector
Syntax:
dist(vect, method = “binary”, diag = TRUE or FALSE, upper = TRUE or FALSE)
Example: Binary distance
R
# R program to illustrate how to calculate # Binary distance # using dist() function # Initializing a vector vect1 <- c (1, 4, 8, 9, 2, 3) # Initializing another vector vect2 <- c (9, 4, 1, 2, 4, 7) # Initializing another vector vect3 <- c (1, 7, 9, 3, 2, 8) # Initializing another vector vect4 <- c (2, 1, 4, 7, 8, 9) # Initializing another vector vect5 <- c (1, 4, 8, 3, 9, 2) # Initializing another vector vect6 <- c (3, 7, 8, 6, 5, 9) #Row bind vectors into a single matrix twoDimensionalVect <- rbind (vect1, vect2, vect3, vect4, vect5, vect6) print ( "Binary distance between each pair of vectors is: " ) cat ( "\n\n" ) # Calculate Binary distance between vectors using # built in dist method By passing two-dimensional # vector as a parameter Since we want to calculate # Binary distance between each unique pair of vectors # That is why we are passing Binary as a method dist (twoDimensionalVect, method = "binary" , diag = TRUE , upper = TRUE ) |
Output:
How to Use Dist Function in R?
In this article, we will see how to use dist() function in R programming language.
R provides an inbuilt dist() function using which we can calculate six different kinds of distances between each unique pair of vectors in a two-dimensional vector. dist() method accepts a numeric matrix as an argument and a method that represent the type of distance to be measured. The method must be one of these distances – Euclidean, Maximum, Manhattan, Canberra, Binary, and Minkowski. It accepts other arguments also but they are optional.
Syntax:
dist(vect, method = ” “, diag = TRUE or FALSE, upper = TRUE or FALSE)
Parameters:
- vect: A two-dimensional vector
- method: The distance to be measured. It must be equal to one of these, “euclidean”, “maximum”, “manhattan”, “canberra”, “binary” or “minkowski”
- diag: logical value (TRUE or FALSE) that conveys whether the diagonal of the distance matrix should be printed by print.dist or not.
- upper: logical value (TRUE or FALSE) that conveys whether the upper triangle of the distance matrix should be printed by print.dist or not.
Return type:
It return an object of class “dist”
Now let us see how to calculate these distances using dist() function.