Calculating binary distance in R to Get the Distance between Rows
Two vectors share a part of their elements in between. Sometimes, this proportion of elements shared maybe 0. The binary distance illustrates this. The input vectors are specified, and then the binary distance between them is calculated using the dist() method in R with a method equivalent to “binary.”
Syntax: dist(vect, method = “binary”, diag = TRUE or FALSE, upper = TRUE or FALSE)
R
# Creating matrix matr <- matrix (1:12, nrow = 4) print ( "Original Matrix" ) print (matr) # Calculating the distance between # rows of matrix print ( "Maximum Distance between rows of matrix" ) dist (matr,method= "binary" ) |
Output:
[,1] [,2] [,3] [1,] 1 5 9 [2,] 2 6 10 [3,] 3 7 11 [4,] 4 8 12 [1] "Maximum Distance between rows of matrix" 1 2 3 2 0 3 0 0 4 0 0 0
Distance Between Rows in R
In this article, we will learn various approaches to calculating the distance between the given rows in the R programming language.
The dist() function in R is used to calculate a wide range of distances between the specified vector elements of the matrix in R. The default method for distance computation is the “Euclidean distance,” which is widely used in mathematics. It has the following syntax :
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”