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.

2 comments: