## Saturday, November 3, 2018

### 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.

1. This comment has been removed by a blog administrator.

2. informative article...!
data science course in noida

3. I am stunned by the information that you have on this blog. It shows how well you fathom this subject.
hrdf training course

4. I think I have never watched such online diaries ever that has absolute things with all nuances which I need. So thoughtfully update this ever for us.
360DigiTMG data analytics course malaysia

5. Super site! I am Loving it!! Will restore again, Im taking your food in like manner, Thanks.
difference between analysis and analytics

6. Wow, amazing post! Really engaging, thank you.