I have latitude and longitude data for different points. This is a simple version of my data:
# Add library
library(tidyverse)
# Generate data
distance <-
tibble(
location = c("first", "second", "third"),
lat = c(33.720792, 33.715187, 33.714848),
long = c(-84.468126, -84.468684, -84.454265)
)
Which produces data that looks like this:
# A tibble: 3 x 3
location lat long
<chr> <dbl> <dbl>
1 first 33.7 -84.5
2 second 33.7 -84.5
3 third 33.7 -84.5
What I'd like to do is take these latitude and longitude to get the full ranking of nearest neighbors for each location. For example, ideally my final data would look like this:
As you can see the first column in this new data frame contains the first nearest neighbor to location "first", the second column provides the next nearest neighbor, and so on.
Does anyone know how I can make the data frame that I need?