I have two data frames. One has ~600 or so locations of people on a map, like so:
Names<-c("Mike","Ron","Joe")
Lat<-c(61.5,63.2,50.1)
Long<-c(-140.2,-139.2,-145.6)
df<-data.frame(Names,Lat,Long)
The other has locations those people might need to get to:
Places<-c("ClinicA","ClinicB","ClinicC")
Lat<-c(58.5,50.2,40.1)
Long<-c(-135.2,-133.2,-140.6)
df1<-data.frame(Places,Lat,Long)
I know that if I have an individual set of coordinates I can use googleway to get the distances in between like so:
df <- google_distance(origins = list(c("Melbourne Airport, Australia"),
c("MCG, Melbourne, Australia"),
c(-37.81659, 144.9841)),
destinations = c("Portsea, Melbourne, Australia"),
key = key)
My question is, I'd like to go through each individual person, calculate the distance to each of the locations of the places (i.e. return three different distances for each person), and then only keep the smallest distance for each. The resulting data frame would hopefully be:
Rows of: Name, lat,long,NearestPlace,Distance to that place
I'm assuming it'd maybe be a "for" loop, but I'm kinda lost at this point. Any help would be appreciated, thank you!