Distance Between Two Sets of Points in R
Last Updated :
23 Sep, 2022
In the following article, we are going to compute the distance between two sets of points in the R programming language.
In algebra, each point in the two-dimensional plane is represented by the x and y coordinates respectively. The distance between any two points can be computed using both of its coordinate points. There are a large number of in-built as well as external packages in R which can be used to compute the distance between two sets of points. In this article, we are going to use the dist() and crossdist() function to calculate the distance between two sets of points.
Example 1: Computing Euclidean Distance
The proxy library in R is used to calculate the proximities and compute them efficiently. The package can be downloaded and installed into the working space using the following command :
install.packages("proxy")
library("proxy")
The dist() function can be used to compute the distance between the specified set of points. It returns the distance matrix of the specified matrix using the method that is specified as the argument.
Syntax: dist(df1, df2, method)
Parameters :
- df1 – The first data frame
- df2 – The second data frame
method – The method used to compute distance, which may be “euclidean”, “manhattan” or “maximum” distance.
R
library (proxy)
data_frame1 = data.frame (x= c (2, 4),
y= c (3, 1))
data_frame2 = data.frame (x= c (8, 2),
y= c (6, 3))
print ( "Distance Matrix " )
dist (data_frame1, data_frame2,
method= "euclidean" )
|
Output:
[1] "Distance Matrix "
> #creating a distance matrix
> dist(data_frame1, data_frame2, method = "euclidean")
[,1] [,2]
[1,] 6.708204 0.000000
[2,] 6.403124 2.828427
In the above code, firstly we have defined two data frames of different coordinates and after that compute the distance between the two points using euclidean method in dist() function.
R
pt1 <- data.frame (x = 2,
y = 1)
pt2 <- data.frame (x = 10,
y = 1)
euc.dist <- function (x1, x2) sqrt ( sum ((x1 - x2) ^ 2))
dist<- euc.dist (pt1,pt2)
print ( "Euclidean Distance" )
print (dist)
|
Output:
[1] "Euclidean Distance"
> dist
[1] 8
Example 2: Using crossdist()
In this example, We are going to determine the distance between two points by using the crossdist() function available in R. The spatstat .geom package in R can be installed using the following command :
install.packages("spatstat.geom")
The crossdist() function is used to calculate pairwise distances between two different sets of points.
Syntax: crossdist(x1, y1, x2, y2)
Parameters :
- x1, y1: Numeric vectors indicating the coordinates of the first set of points.
- x2, y2: Numeric vectors indicating the coordinates of the second set of points.
The distance between the two points indicative of their coordinates is calculated according to the formula: √((x2 – x1)2 + (y2 – y1)2)
R
library ( "spatstat.geom" )
x1 = 2
y1 = 1
x2 = 3
y2 = 1
dist = crossdist (x1, y1, x2, y2)
print ( "Distance between points" )
print (dist)
|
Output:
[1] "Distance between points"
[,1]
[1,] 1
In the above code, firstly we have defined two coordinates using variables x1, y1, x2, and y2 after that compute the distance between these two coordinates by passing these variables as arguments in crossdist() function.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...