So I have a formula for calculating the distance between two locations,however I want to have a second function that uses this one. The second function would get a list of lat, long values with corresponding numbers 1,2,3,etc.Then for for each point on the list run the GPSDist on all the other lat long data in that column. find the set that has the smallest distance (d) value, and say what number that lat long corresponds to.
GPSDist <- function(lon1,lat1,lon2,lat2,u){
if (u=="k"){R<-6373}
if (u=="m"){R<-6378137}
if (u=="M"){R<-3961}
if (u=="k"){print("unit = kilometers")}
if (u=="m"){print("unit = meters")}
if (u=="M"){print("unit = miles")}
radians <- 0.0174532925
lon1 <- lon1* radians
lat1 <- lat1* radians
lon2 <- lon2* radians
lat2 <- lat2* radians
dlon <- lon2-lon1
dlat <- lat2-lat1
a <- ((sin((dlat)/2))^2) + cos(lat1) * cos(lat2) * ((sin((dlon)/2))^2)
c <- 2 * atan2(sqrt(a), sqrt(1-a))
d = c*R
return(d)
}
for clarity I would like use that function (GPSDist) on each point on say there is a data set 1 to 5; use point 1 against points 2,3,4,5. find smallest list it by its respective number. use point 2 compare to points 1,3,4,5 etc.