## Saturday, November 3, 2018

### Distance Matrix Example in R (Manhattan Method)

As some of you may hard to find a good example of distance matrix example in R with its explanation, I try to fill the gap with this article. Hope you will find it useful.

We can use dist function in R to calculate distance matrix with Manhattan method - which simply sum the differences of points observed.

Let's follow the examples below to understand how to use it. Along the example, we will use data frame variable named datatocalculate.

> datatocalculate <- data.frame( x = c(3, 1), y = c(2, 4) )

> datatocalculate
x y
1 3 2
2 1 4

Now, let's calculate the distance matrix.

> dist(datatocalculate, method="manhattan")
1
2 4

The calculated distance from 1 to 2 is 4, comes from the following formula:

|1-3| + |4-2|
|-2| + |2|
2 + 2
4

We will modify datatocalculate variable with the following new values.

> datatocalculate <- data.frame( x = c(3, 2, 1), y = c(2, 4, 8) )

datatocalculate
x y
1 3 2
2 2 4
3 1 8

Calculate the distance matrix again.

> dist(datatocalculate, method="manhattan")
1  2
2  3
3  8  5

The calculated distance from 1 to 2 is 3. It is a result of the following calculation:

|2-3| + |4-2|
|-1| + |2|
1 + 2
3

The calculated distance from 1 to 3 is 8. It is a result of the following calculation:

|1-3| + |8-2|
|-2| + |6|
2 + 6
8

The calculated distance from 2 to 3 is 5. It is a result of the following calculation:

|1-2| + |8-4|
|-1| + |4|
1 + 4
5

That's all. Hope this short article will make you clear on the usage of dist function with Manhattan method in R.

### Distance Matrix Example in R (Euclidean)

As some of you may hard to find a good example of distance matrix example in R with its explanation, I try to fill the gap with this article. Hope you will find it useful.

We use dist function in R to calculate distance matrix, with Euclidean distance as its default method.

As you recall, the Euclidean distance formula of two dimensional space between two points is:

sqrt( (x2-x1)^2 + (y2-y1)^2 )

The distance formula of three dimensional space between two points is:

sqrt( (x2-x1)^2 + (y2-y1)^2 + (z2 - z1)^2 )

Now, let's practice with its implementation in R. Along the example, we will use data frame variable named datatocalculate.

> datatocalculate <- data.frame( x = c(1, 2), y = c(2, 4) )

> datatocalculate
x y
1 1 2
2 2 4

Now, let's calculate the distance matrix.

> dist(datatocalculate)

The output should be like the following - with additional colors added to make it easy to explain later.

1
2 2.236068

The calculated distance from 1 to 2 is 2.236068. It is a result of the following calculation.

sqrt((2-1)^2 + (4-2)^2)
sqrt(5)
2.236068

We will now modify datatocalculate with the following new values.

> datatocalculate <- data.frame( x = c(1, 2, 1), y = c(2, 4, 6) )

datatocalculate
x y
1 1 2
2 2 4
3 1 6

Calculate the distance matrix again.

> dist(datatocalculate)
1        2
2 2.236068
3 4.000000 2.236068

The calculated distance from 1 to 2 is 2.236068. It is a result of the following calculation:

sqrt((2-1)^2 + (4-2)^2)
sqrt(5)
2.236068

The calculated distance from 1 to 3 is 4.000000. It is a result of the following calculation:

sqrt((1-1)^2 + (6-2)^2)
sqrt(16)
4.000000

The calculated distance from 2 to 3 is 2.236068. It is a result of the following calculation:

sqrt((1-2)^2 + (6-4)^2)
sqrt(5)
2.236068

That's all. Hope this short article will make you clear on the usage of dist function in R.