Sorry this will be a question for the cycle. I'm struggling to write a simple loop to create a table of distances between cities based on latitude longitude data
locations <-read.csv("distances.csv")
returns the following table:
City Type long lat
1 Sheffield EUR -1.470085 53.38113
2 HK WRLD 114.109497 22.39643
3 Venice EUR 12.315515 45.44085
4 New York WRLD -74.005941 40.71278
My goal in this particular part of the task is to create a table of distances (in kilometers) between each of the cities in the nature of a correlation matrix with a diagonal of 0 (i.e., all cities at a zero distance from themselves).
To do this, I use the sp package, which requires a long-lat matrix of values, so I can remove the text as follows:
datmax <- data.matrix(locations)
datmax2 <- datmax[,-1:-2]
spDistsN1 , , . , ( № 1):
km <- spDistsN1(datmax2, datmax2[1,], longlat=TRUE)
:
[1] 0.000 9591.009 1329.882 5436.133
, , , for:
for (i in 1:nrow(datmax2)){
kmnew <- spDistsN1(datmax2, datmax2[i,], longlat=TRUE)
}
NY:
[1] 5436.133 12967.023 6697.541 0.000
, . , , . .