I need to find the distance between one pair if we have latitude and longitude . also with condition if any of the variables are null then we should have result as Null
and if we have values den it should give the distance.. How to create loop for the same ?
Pairs Latitude Longitude
16285 50.0354 19.2343
16285 50.0748 19.9125
16283
16283
16281
16281
16279
16279
16277
16277
16275
16275
16273
16273
16271 51.7549 19.463
16271 50.5908 21.1178
16269 33.5912 130.4013
16269 34.8234 134.8785
Pairs Latitude Longitude distance
16285 50.0354 19.2343 500
16285 50.0748 19.9125 500
16283
16283
16281
16281
16279
16279
16277
16277
16275
16275
16273
16273
16271 51.7549 19.463 200
16271 50.5908 21.1178 200
16269 33.5912 130.4013 100
16269 34.8234 134.8785 100
How can I achieve this via Loop
Heading
lat1 = as.numeric(fd2[3,3]) lat2 = as.numeric(fd2[4,3])
long1 =as.numeric(fd2[3,4])
long2 =as.numeric(fd2[4,4])
earth.dist <- function (long1, lat1, long2, lat2) { rad <- pi/180
a1 <- lat1 * rad
a2 <- long1 * rad
b1 <- lat2 * rad
b2 <- long2 * rad
dlon <- b2 - a2
dlat <- b1 - a1
a <- (sin(dlat/2))^2 + cos(a1) * cos(b1) * (sin(dlon/2))^2
c <- 2 * atan2(sqrt(a), sqrt(1 - a))
R <- 6378.145
d <- R * c
return(d) }
dist_km <- earth.dist(long1,lat1,long2,lat2)
dist_mi <- dist_km / 1.609344
myout <- data.frame("dist_km" = dist_km, "dist_mi" = dist_mi)
Condition: Check the pair are equal associated Latitude and Longitude shouldn't be null if any of the value is there then it should show null . if all condition satisfy then yes.