Below dataframe contains the information about the lat, long , state and city. I want to find the three nearest cities for every city given in the dataframe. For example, from the below dataframe, Oklahoma city and Colarado SPringd nearest to Albuquerque, So three nearest city to Albuquerque should be saved in other dataframe named nearest_AL(I don't know how to get this result, that'y I tried to gave an idea by creating a data frame).
dataframe<-data.frame(long=c("-106.61291","-81.97224","-84.42770","-72.68604","-97.60056","-104.70261"),
lat=c("35.04333","33.37378","33.64073","41.93887","35.39305","38.80171"),
state=c("NM","GA","GA","TX","OK","CO"),
city=c("Albuquerque","Augusta","Atlanta","Windsor Locks","Oklahoma City","Colarado Springs")
)
nearest_Al<-data.frame(long=c("-97.60056","-104.70261"),
lat=c("35.39305","38.80171"),
state=c("OK","CO"),
city=c("Oklahoma City","Colarado Springs")
)
This same thing I have to perform on the dataframe which contains rows 500k and around 100 locations.
Thanks in advance!
This might be a little slow with all your data but it does the trick
The following should work for you
I made a
distance
function that acceptsx
(longitude of current row indataframe
),y
(latitude of current row indataframe
), anddataframe
. It returns the top 2 nearest cities (excluding the target city)tidyverse solution
To save only the nearest cities as a separate data frame
Output
Extra
If you want to keep the original database and the nearest cities
Extra outputSplit into named list
Here is one idea.
dataframe2
is the final output. TheNear_City
column shows the top three closest cities for each city in thecity
column.Update
We can further create the output the OP wants.
Now each "Target City" is an element on the list
nearest_city_list
. To Access the data, we can access the list element using the target city name. Here is an example pulling out the results of Albuquerque: