Minimum distance between coordinates. How assign closest coordinates

Hi, I am trying to create a loop using "for" to calculate the minimum distances between points (in coordinates Latitude - Longitude) in two dataframes

In my case I have a dataframe1 with more than 1,000 points and another (dataframe2) with more than 10,000 points, and I need to assign closest points in dataframe2 to each point in dataframe1

The distance between two points is calculated using the formula:

Formula

This is an example of dataframes with coordinates:

coordinates1 <- data.frame("X1"=c(70,10,50), "Y1"=c(20,5,60))

coordinates2 <- data.frame("X2"=c(1,2,45,7,8,5,4,13,3,6), "Y2"=c(2,5,36,4,3,2,6,27,9,8))

Could someone help me to solve it?

Thanks in advance

Javi (from Madrid, Spain)

Hi Javi,

How about this?

dist_func = function (x1, y1, x2, y2){
    dist = sqrt(((x2 - x1)^2) + ((y2-y1)^2))
    return(dist)
}

# Container columns for closest point
df1$x_closest = NA
df1$y_closest = NA

for(i in 1:nrow(df1)){
    min_dist = 0
    x1 = df1$x[i]
    y1 = df1$y[i]
    for(j in 1:nrow(df2)){
        x2 = df2$x[j]
        y2 = df2$y[j]
        current_dist = dist_function(x1, y1, x2, y2)
        if(j == 1){
            min_dist = current_dist
            x_closest = x2
            y_closest = y2
        }
        if(current_dist < min_dist){
            min_dist = current_dist
            x_closest = x2
            y_closest = y2
        }
    }
    df1$x_closest[i] = x_closest
    df1$y_closest[i] = y_closest
}

Hi rossdrucker9! thanks for your help!

I am trying to use it but I am not capable to solve it... it give me back an error:

did it work to you with my example?

Thanks again!


df1 <- data.frame("x1"=c(70,10,50), "y1"=c(20,5,60))

df2 <- data.frame("x2"=c(1,2,45,7,8,5,4,13,3,6), "y2"=c(2,5,36,4,3,2,6,27,9,8))


dist_func = function (x1, y1, x2, y2){
    dist = sqrt(((x2 - x1)^2) + ((y2-y1)^2))
    return(dist)
}


# Container columns for closest point


df1$x_closest = NA
df1$y_closest = NA

for(i in 1:nrow(df1)){
    min_dist = 0
    x1 = df1$x[i]
    y1 = df1$y[i]
    for(j in 1:nrow(df2)){
        x2 = df2$x[j]
        y2 = df2$y[j]
        current_dist = dist_func(x1, y1, x2, y2)
        if(j == 1){
            min_dist = current_dist
            x_closest = x2
            y_closest = y2
        }
        if(current_dist < min_dist){
            min_dist = current_dist
            x_closest = x2
            y_closest = y2
        }
    }
    df1$x_closest[i] = x_closest
    df1$y_closest[i] = y_closest
}

I got it! Thanks Rossdrucker9!

df1 <- data.frame("x1"=c(70,10,50), "y1"=c(20,25,60))

df2 <- data.frame("x2"=c(1,2,45,7,8,5,4,13,3,6), "y2"=c(2,5,36,4,3,2,6,27,9,8))

dist_func = function (x1, y1, x2, y2){
    dist = sqrt(((x2 - x1)^2) + ((y2-y1)^2))
    return(dist)
}

# Container columns for closest point
df1$x_closest = NA
df1$y_closest = NA
df1$dist = NA

for(i in 1:nrow(df1)){
    min_dist = 0
    x1 = df1$x1[i]
    y1 = df1$y1[i]
    for(j in 1:nrow(df2)){
        x2 = df2$x2[j]
        y2 = df2$y2[j]
        current_dist = dist_function(x1, y1, x2, y2)
        if(j == 1){
            min_dist = current_dist
            x_closest = x2
            y_closest = y2
        }
        if(current_dist < min_dist){
            min_dist = current_dist
            x_closest = x2
            y_closest = y2
        }
    }
    df1$x_closest[i] = x_closest
    df1$y_closest[i] = y_closest
    df1$dist[i] = min_dist
}

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.