I am a casual user of R language, I have a dataset of 800000 coordinates (longitude, latitude) for postal codes in Canada.
I have attached a CSV file with a sample called question.csv.
TariffZone<-c("CAAL1","CAAL2","CAAL3","CAAL3")
latitude<-c(51.15701,51.91826,50.80972,50.76358)
longitude<-c(-110.206392,-110.146722,-110.7003,-110.568045)
elevation<-c(300,400,450,450)
d<-data.frame(TariffZone,latitude,longitude,elevation)
I am trying to get a table where I would have all the distances in KM in between Tariff Zones (which average the coordinates of each postal codes)
I have seen a related question : R distance matrix build
Unfortunately I have not been able to solve my problem with the given answer, here is my code:
d<-read.csv("question.csv", header=TRUE)
head(d)
d2<-d %>%
group_by(TariffZone) %>%
summarise(latitude_mean=mean(latitude),longitude_mean=mean(longitude),elevation_mean=mean(elevation)) %>%
dplyr::mutate_if(is.numeric, format, 4) %>%
ungroup()
head(d2)
d3<-data.frame(d2$longitude_mean,d2$latitude_mean)
head(d3)
distable<-dist(cbind(d2$longitude_mean,d2$latitude_mean),cbind(d2$longitude_mean,d2$latitude_mean),method=euclidean)
harvesine<-function(lat1,lat2){
newVar<-acos(sin(lat1*3.14159265359/180)*sin(lat2*3.14159265359/180) + cos(lat1*3.14159265359/180)*cos(lat2*3.14159265359/180)*cos(lon2*3.14159265359/180-lon1*3.14159265359/180) ) * 6371000}
Please do not hesitate if I forgot to add any piece of information or if I have to prescise my question.
Bonus: If there is a way I can use the harvesine formula I have in the code instead of euclidean and if I could export this table back to a csv file