1

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.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • 1
    Can you provide your data in an easy to paste form ([hints here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example))? What is the expected result given the input data? – Roman Luštrik Aug 28 '19 at 11:41
  • Possible duplicate of [R - multiple loop counter variables](https://stackoverflow.com/questions/48788983/r-multiple-loop-counter-variables) – James Martherus Aug 28 '19 at 11:46

1 Answers1

0

I think the "answers" are to first understand that there is no value named exactly Null that can exist in an R numeric vector. You are needing to understand that NA will serve the purpose of what you were calling Null. It is unclear what form your data is in at the moment, but if it were the case that your Latitude and Longitude columns were in "character" class, then as.numeric would have turned any blank character values into NA values. Then any arithmetic calculation that involved an NA value would have automatically resulted in an NA result. So there would not be any problem with how R operated and your goals.

dat <- read.table(text="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
", header =TRUE, fill=TRUE)

This actually gives you all numeric columns so the as.numeric operations are not needed (although they do not cause any harm, either.).

 str(dat)
'data.frame':   18 obs. of  3 variables:
 $ Pairs    : int  16285 16285 16283 16283 16281 16281 16279 16279 16277 16277 ...
 $ Latitude : num  50 50.1 NA NA NA ...
 $ Longitude: num  19.2 19.9 NA NA NA ...

Then we need to put your function code into a form that can be cutr and pasted into a console session, which I have done by editing your question body. The some testing for its behavior.

The first two lines are not particularly close, so it's unclear what you were expecting for distances in that distance column:

> earth.dist(dat[1,2], dat[1,3],dat[2,2],dat[2,3])
[1] 75.60999
> earth.dist(dat[1,2], dat[1,3],dat[1,2],dat[1,3])
[1] 0

I have not checked the correctness of that d algorithm but it appears to deliver sensible values for differences in lattitutde of 1 degree:

 earth.dist(1, 1,1,2)
[1] 111.3196

It appears you might have wanted to test to see if sequential lines were close to each other, in which case using the all.equal function would have allowed testing when using a "tolerance" value to return TRUE when points were close but not exactly numerically equal.

IRTFM
  • 258,963
  • 21
  • 364
  • 487