Using an API to calculate distance between two airports (two columns) within R?

后端 未结 3 1693
轮回少年
轮回少年 2021-02-09 17:46

I was wondering whether there was a way to compare airport distances(IATA codes). There are some scripts but not is using R. So I tried that with with the API:

developer

3条回答
  •  情话喂你
    2021-02-09 18:20

    You can do this without an API, just using the co-ordinates of airports, which are available freely in a database here https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat

    ## Import airport data
    airports <- read.csv("https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat", header = F)
    
    library(geosphere)
    ## Calculate matrix of differences in km
    distance.matrix <- distm(x = airports[,c("V8", "V7")],  y = airports[,c("V8", "V7")])/1000
    ## This may take a while, and will generate a matrix 8107 by 8107, about 0.5gb in size.
    
    ## Rename dimensions to airport codes
    rownames(distance.matrix) <- airports$V5
    colnames(distance.matrix) <- airports$V5
    
    ## Example: km between Gatwick and Heathrow
    distance.matrix["LGW", "LHR"]
    [1] 41.24091
    

提交回复
热议问题