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.

5 comments:

  1. Ayoan Casino and Resort - JCMH
    JCMH provides guests with great perks 동해 출장샵 like free casino, free online slots, and sports 구리 출장안마 betting. JCMH, 포항 출장마사지 JCMH, Casino, Resort, 밀양 출장마사지 Casino, 부천 출장마사지 Resort.

    ReplyDelete
  2. The MLF weighs 2 lbs lighter than its predecessor, the NICE Frame, by using completely different supplies and development methods and lowering the profile of the wing and belt. Finally, the vendor may offer you insurance, but that is all the time a bad guess and shouldn’t be taken. Super Blackjack Battle II Turbo Edition introduces 카지노사이트 a roster of 12 very completely different characters all competing to turn out to be the greatest Blackjack participant of all time.

    ReplyDelete
  3. NHL Predictions – our free NHL picks are the place it's at for sports bettors. The more likely consequence is marked as adverse, indicating that it requires a bigger bet to generate income when you bet on the favored possibility. In a cash line bet, if a team’s odds are -150, this implies they are more {likely to|more likely to|prone to} win and it'll require a bet of $150 to receive a $100 payout if they that they} win. A spherical robin is a series of smaller parlays created 코인카지노 from a bigger record of bets.

    ReplyDelete
  4. This printer is perfect for folks just starting 3D printing and people who have quantity of} years underneath their belt. best travel underwear For a little over $900, the printer can rival printers 2-3 times its price. Inbuilt calibration, error detection, and a big neighborhood will assist you to get the precise print you actually want}.

    ReplyDelete