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

后端 未结 3 1688
轮回少年
轮回少年 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:33

    What about the following loop over your api calls?

    df$distance <- 0
    
    for (i in nrow(df)){
      drs <- df[i,]$departure
      fue <- df[i,]$arrival
      url <- paste0("https://airport.api.aero/airport/distance/", drs, "/", fue, "?user_key=4e816a2bf391f8379df1c42d2762069e")
      api <- curl_fetch_memory(url)
      text <- rawToChar(api$content)
      distance <- as.numeric(gsub(',','',substr(text,regexpr('distance',text)+11,regexpr('units',text)-4)))
      df[1,]$distance <- distance
    }
    
    df
    

提交回复
热议问题