【问题标题】:Trying to determine the distances between centroids of countries R试图确定国家 R 的质心之间的距离
【发布时间】:2020-07-16 19:18:12
【问题描述】:

我目前有一个国家/地区代码对的数据框(例如 US、RU、CA 等)。是否有一个函数可以根据国家/地区代码确定国家/地区的质心,以便我可以找到国家/地区对之间的距离?或者有没有一个函数可以给我每个国家质心的坐标(比如经度和纬度)?

这是我从前一个数据集过滤出来的数据集的前几行以供参考。

【问题讨论】:

    标签: r distance country-codes centroid


    【解决方案1】:

    你可以抓取这个google public dataset
    我之前建议使用 CoordinateCleaner 包中的 countryref 数据集不起作用,因为我发现有不同位置的重复项。

    library(rvest)
    library(dplyr)
    
    url <- 'https://developers.google.com/public-data/docs/canonical/countries_csv'
    webpage <- read_html(url)
    centroids <- url %>% read_html %>% html_nodes('table') %>% html_table() %>% as.data.frame
    
    data <- data.frame(V1 = c("US","US"), V2 = c('VN','ZA'))
    data %>% inner_join(centroids,by = c("V1"="country")) %>% inner_join(centroids,by = c("V2"="country"))
    
      V1 V2 latitude.x longitude.x        name.x latitude.y longitude.y       name.y
    1 US VN   37.09024   -95.71289 United States   14.05832   108.27720      Vietnam
    2 US ZA   37.09024   -95.71289 United States  -30.55948    22.93751 South Africa
    

    【讨论】:

    • 通过查看包详细信息,我的理解是包包含一个数据集,然后我可以使用它映射到我自己的数据框中的国家代码。这是正确的吗?
    • 我想是的,如果您在问题中提供数据集示例,我们可以检查这一点:dput(head(yourdataset))
    • 如果有帮助,我添加了数据集前几行的图片。
    • 在尝试整合您的数据集后,我发现countryref 有重复项,请参阅我提出的替代方案。顺便说一句,在共享数据集时,最好复制 dput(head(dataset)) 的结果而不是放置图像,因为我可以在我的代码中立即复制它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-14
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 2017-10-26
    • 2019-06-11
    • 2021-09-23
    相关资源
    最近更新 更多