If you are working on a project where you need to calculate the distance between different cities or location
or you want to find out the nearest location to your target city or you want to find the number
of locations or different cities that are within a specific radius. Then the command you can use is geologist
If you haven't installed this command, you can install it by using access installed
Judas. Judas is the command name and the data we are going to use is distance data set which I have created and it
contains the location of latitude and longitude of different cities in us
You can download this dataset from the link given in description, let's load this data
And now you can see we have three variables. The first one is the city name
the latitude and the longitude. So first let me show you direct set
So we have the city name, the latitude and the longitude. The Syntex of Judas is quite simple
We write the command name, then the latitude and longitude of the first location and latitude longitude of the
second location. Remember latitude would come first followed by longitude and if an end statements are
option, then we need to have an option of generate where we would give the name of the variable that would be generated
Another option options are optional. So starting with, I have the longitude and attitude of New York City
and I want to calculate the distance between different cities of us and the New York City
So in this case, I would first tried the latitude and then the longitude of New York and the latitude longitude of
other cities are stored in these specific variables. We want to generate a new variable
And this is this option is not optional. We have to specify the generate option
And within para this is we would write the name of the variable
whichever variable we want to create. So it can be anything new variable
new distance. In this case, I am writing the two alphabets key and which stands for kilometers
So by default, this command would generate distances in kilometers. So you can see that we have latitude longitude and
then we have the distance between these two cities. We can see that the distance between Dallas and in New York is 20 to 14
kilometers. Now, this is not the distance by road. It is the flight distance or air distance or
what we say as the crow flies. So a quick Google search will tell you that the distance
the flight distance between Dallas and New York is 22 40. So we can assume that it is fairly accurate
And the by road distance between Dallas and New York is somewhat 2400 kilometers
So it isn't the by road distance, it is the distance by air
So what if instead of the distance in kilometers, we wanted it in miles
we would simply add the option of mile and see, I have used the variable name
which variable you want to name it. So now instead of the distance in kilometers
we have it in miles. Remember the distance between New York City with itself is obviously zero
Now remember there are different formulas to calculate the distance. There is this formula have worsen formula
And then we have incentive formula. The default of this, the hardest command is this incentive formula
But we can use person if we wanted to for that, we would have to use an additional option of fear
So whether we want to calculate kilometers or miles, we can use this fear option to use this specific formula instead of the default one
So let me run these commands and you can see it has calculated the kilometers and the miles using the second formula
They are fairly close enough. So one can conclude that there isn't much difference between them
So what if our latitude and longitude of this New York City was stored in a variable instead of inputting
these specific values by themselves. So let me create two variables with based attitude and based longitude and the based
attitude will restore the latitude of New York City. And the based long issued would store the longitude of the New York City
We generate them. Remember whenever you are working with distances or Judas command
you want to store the values in double type format rather than the float format
And the double is more precise as compared to the floor format
So in this case, when we generated these variables, the type of variable is is flowed
So it would generate an issue and let me share that issue with you
Now, instead of using the specific values of the base latitude and longitude
we are, we have replaced it with the variable names
Now, what happens is that the difference between New York City with itself is not zero
not strictly speaking zero. And that is because the are this based accurate variable is a float variable
Where is our latitude? Variable is a double variable. It is more precise and we have lost some precision over here
So this was also to demonstrate that whenever you are saving your latitude and longitude variable
you want to save them in in double format rather than a float format
Okay, let me load another dataset and this data set have only four cities
Let's just say let's assume that these are industrial cities and these falsities are Boston
Iowa City, Houston. And then we have Chicago and what I want is all those cities from my previous data file
That contain all the cities of us and their latitude and longitude
What I want is to have the all the cities that are within the 30 km distance of these four cities
say I want to find the city's closer to the industrial cities
Now, for that, we first need to make a pair wise combinations and for pair wise combinations instead of we have a command which
is called cross. This command is used for pair wise combination
Before using this command, we have two files. One is the industrial city file and the second is the distant city file
Remember in distant city file, we we also have the same variables
the city, the latitude and the longitude in this industrial city file
I have renamed the variable by adding a suffix one to them
So we have a different variable names. So I run the cross command and it will generate all the pair wise combination
So now you can see we have all the cities combination with Boston and then we have all the cities in front of Iowa City
and so on and so forth. So we have made all the pair wise combinations
and then we want to calculate the distance between all these pair wise combinations
This is our distance variable. Now we want to keep only the cities that are within 30 km
of our city one variable. So we use by sort city one
And then we keep, if the distance, the variable name distance is less than 30
So we have all these variables where we and the distance is less than 30 km
But you would note here that uh zero distance between Boston itself or the zero distance between the Chicago itself is also including
this dataset. And we would simply um we would have to delete them by dropping the variables that have zero
value. So now we have understood how to get the list of cities that are within 30 km of our base cities
But what if we wanted to have just the nearest cities to our base cities
So in this case, we just wanted to have say this is the closest city with Boston
which is Brooklyn and the closest city with Chicago is
say Siro. So what we want is we want to have a nearest city using this dearest command
Let me reload this data. So we have a clean copy of the data set
We have all these four cities, their latitude and the longitude. These are our base cities
We again make the pair wise combinations. It is similar to our previous command that we have used
we calculate the distance
So till now, we are simply using the previous commands. Now we saw the variable by the city
one and the distance R Bay city and the distance variable. So first we would have the Bay City all the combinations of Boston
and then we have would have all the combinations of Houston and so on and so forth
And within Boston, the lowest distance would come first and, and then it would be in ascending
order. So this is how the reactor would look like we would have first
all the combination of Boston and the distance would be in ascending order
But we want to drop this first value for each based city because they are the distance
with themselves and they would always be zero. So in case of Boston
this this first value is zero
When we scroll down
the first value of Chicago is also zero. So we want to drop all the cases where the Bay City is equal to this city
variable. So in this case, when Chicago is equal to Chicago, these two variables matches this specific road would be deleted
And now the first observation, the first row for each Bay City would be the nearest distance to them
And we simply keep only the first row for each Bay City
So now we have this clean data set
where the closest city to Boston is Brooklyn
It is within 2.5, 4 or five km of Boston. The closest city to Iowa City is and Musa
And now we have all these cities that are nearest to the to our base cities