如何在R中将地区名称或代码转换为经度和纬度

问题描述 投票:0回答:1

我有一个包含国家名称及其 iso3c 代码的数据框,我想添加两列:经度和纬度,以便我可以将我的数据放到带有 highcharter 的交互式地图上。我无法弄清楚要使用什么包以及如何创建这些列。

what my df currently looks like

我尝试使用“maps”包,但我不知道我是否正确使用它,因为我仍然无法获取新列。

rstudio visualization r-highcharter
1个回答
0
投票

一种方法:

### Packages
library(readr)
library(dplyr)

### Data
data = structure(
  list(
    coName = c(
      "Albania",
      "Argentina",
      "Armenia",
      "Austria",
      "Azerbaijan",
      "Bangladesh",
      "Bulgaria",
      "Central African Republic",
      "Switzerland",
      "Chile",
      "Colombia",
      "Costa Rica",
      "Cuba",
      "Cyprus",
      "Djibouti",
      "Denmark",
      "Spain"
    ),
    coCode = c(
      "ALB",
      "ARG",
      "ARM",
      "AUT",
      "AZE",
      "BGD",
      "BGR",
      "CAF",
      "CHE",
      "CHL",
      "COL",
      "CRI",
      "CUB",
      "CYP",
      "DJI",
      "DNK",
      "ESP"
    ),
    region = c(
      "Europe & Central Asia",
      "Latin America & Caribbean",
      "Europe & Central Asia",
      "Europe & Central Asia",
      "Europe & Central Asia",
      "South Asia",
      "Europe & Central Asia",
      "Sub-Saharan Africa",
      "Europe & Central Asia",
      "Latin America & Caribbean",
      "Latin America & Caribbean",
      "Latin America & Caribbean",
      "Latin America & Caribbean",
      "Europe & Central Asia",
      "Middle East & North Africa",
      "Europe & Central Asia",
      "Europe & Central Asia"
    ),
    incGroup = c(
      "Upper middle income",
      "Upper middle income",
      "Upper middle income",
      "High income",
      "Upper middle income",
      "Lower middle income",
      "Upper middle income",
      "Low income",
      "High income",
      "High income",
      "Upper middle income",
      "Upper middle income",
      "Upper middle income",
      "High income",
      "Lower middle income",
      "High income",
      "High income"
    )
  ),
  class = c("tbl_df",
            "tbl", "data.frame"),
  row.names = c(NA,-17L)
)

### Latitude and longitude data (source : Github Tadas Tamošauskas)
geo=read_csv("https://gist.github.com/tadast/8827699/raw/61b2107766d6fd51e2bd02d9f78f6be081340efc/countries_codes_and_coordinates.csv") %>% 
  select(3,5:6)

### Merge
output=pays %>% left_join(y = geo,by = join_by(coCode==`Alpha-3 code`))

输出:

# A tibble: 17 × 6
   coName                   coCode region                     incGroup            `Latitude (average)` `Longitude (average)`
   <chr>                    <chr>  <chr>                      <chr>                              <dbl>                 <dbl>
 1 Albania                  ALB    Europe & Central Asia      Upper middle income                 41                    20  
 2 Argentina                ARG    Latin America & Caribbean  Upper middle income                -34                   -64  
 3 Armenia                  ARM    Europe & Central Asia      Upper middle income                 40                    45  
 4 Austria                  AUT    Europe & Central Asia      High income                         47.3                  13.3
 5 Azerbaijan               AZE    Europe & Central Asia      Upper middle income                 40.5                  47.5
 6 Bangladesh               BGD    South Asia                 Lower middle income                 24                    90  
 7 Bulgaria                 BGR    Europe & Central Asia      Upper middle income                 43                    25  
 8 Central African Republic CAF    Sub-Saharan Africa         Low income                           7                    21  
 9 Switzerland              CHE    Europe & Central Asia      High income                         47                     8  
10 Chile                    CHL    Latin America & Caribbean  High income                        -30                   -71  
11 Colombia                 COL    Latin America & Caribbean  Upper middle income                  4                   -72  
12 Costa Rica               CRI    Latin America & Caribbean  Upper middle income                 10                   -84  
13 Cuba                     CUB    Latin America & Caribbean  Upper middle income                 21.5                 -80  
14 Cyprus                   CYP    Europe & Central Asia      High income                         35                    33  
15 Djibouti                 DJI    Middle East & North Africa Lower middle income                 11.5                  43  
16 Denmark                  DNK    Europe & Central Asia      High income                         56                    10  
17 Spain                    ESP    Europe & Central Asia      High income                         40                    -4  
© www.soinside.com 2019 - 2024. All rights reserved.