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
1 1 2
2 2 4
3 1 6
Calculate the distance matrix again.
> dist(datatocalculate)
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.
This comment has been removed by a blog administrator.
ReplyDeleteinformative article...!
ReplyDeletedata science course in noida
I am stunned by the information that you have on this blog. It shows how well you fathom this subject.
ReplyDeletehrdf training course
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.
ReplyDelete360DigiTMG data analytics course malaysia
Super site! I am Loving it!! Will restore again, Im taking your food in like manner, Thanks.
ReplyDeletedifference between analysis and analytics
Wow, amazing post! Really engaging, thank you.
ReplyDeletemachine learning course in aurangabad