I have roughly 1000 points in one dataset "df1" which have coordinates scattered across the coordinate system. In another dataset "df2", I have 27 points placed across the coordinate system.
For each point in df1, I want to estimate the closest distance to a point in df2, located within the direction of 165-225° (in relation to the unit circle).
How can this be done in R?
If the direction did not matter, then I could solve the problem with the approach from Finding closest point from other data frame
This answer suggest to define your two dataset as data tables and then use the following function:
dist <- function(a, b){
dt <- data.table((df2$x-a)^2+(df2$y-b)^2)
return(which.min(dt$V1))}
results <- df1[, j = list(Closest = dist(x, y)), by = 1:nrow(df1)]
However, I am unsure how to combine this with the direction criteria.
Thank you very much.
Best regards
Ditlev