After messing with my code for a while and fixing error messages, I was able to run the code with no errors, but it did not do what I expected and calculate the distances between each city.
#Separate cities want to find distance between
count = 0
for (i in 1:n_cities) {
{
for (j in i:n_cities-1)
count = count + 1
}
city_1 <- cities3[i,]
city_2 <- cities3[i+count,]
rad <- (pi/180)
distance <- 6371 * acos( (sin(city_1$latitude*rad)*sin(city_2$latitude*rad)) + (cos(city_1$latitude*rad)*cos(city_2$latitude*rad)*cos((city_1$longitude - city_2$latitude)*rad)) )
City.1 <- city_1$city
City.2 <- city_2$city
cities3[[City.1]]
cities3[[City.2]]
cities3[[distance]]
}
I need to find the distance between each city in the data frame, so if I am given 4 cities in my data, I need to find 6 distances. I think my problem is with the counter and running the loop throughout my dataset. Does anyone know what I am doing wrong with the counter and cycling through the loop? I think everything past that should work.