-1

I tried a row by row approach, like this:

library(geosphere)
best <- 1000000
for ( var1 in df1 )
    { findpoint <- c(df1$longitude[var1], df1$latitude[var1])
      for ( var2 in df2 )
        { basepoint <- c(df2$longitude[var2], df2$latitude[var2])
          dis <- distHaversine( basepoint, findpoint, 6378137)
          if (dis < best) {best <- dis
        }  
      print ( best)
    }

and get "Error in .pointsToMatrix(p1) : Wrong length for a vector, should be 2"

I want to use an approach without any external functions so I can confirm the results from an approach using one. Of course I will actually be looking for the best 5 point and recording their info with the df1 data.

  • 1) Can you make this reproducible by including a few lines of example data, e.g. by adding the output of `dput(head(df1))` to the body of your question? 2) I suspect you want `for ( var1 in nrow(df1) )` ... 3) if your data is largish and performance is a concern, it will probably be much faster to structure this into a vectorized calculation instead of a nested loop. – Jon Spring Dec 27 '19 at 01:35
  • Here are two questions about the same error, hopefully helpful: https://stackoverflow.com/questions/35743397/calculate-distance-between-2-lat-longs and https://stackoverflow.com/questions/55217286/calculating-distance-between-two-gps-locations-in-a-data-frame-using-distm-in – Jon Spring Dec 27 '19 at 07:17

1 Answers1

0

Here is code that prints out the 5 closest values to some number in comparison to a value. You could loop if you are doing multiple values.

#data
x=c(1:100)
#value to match to
your.number=5.43

#find differences
diff <- abs(x-your.number)
#find smallest differences
sort <- sort(diff)[1:5]

#find values
for (i in (1:length(sort))){
 print(which(diff == sort[i]))
}
#output
[1] 5
[1] 6
[1] 4
[1] 7
[1] 3

You can also save the values if need be in the loop.

This also may be helpful: r-find two closest values in a vector

megmac
  • 547
  • 2
  • 11