具体来说,我正在尝试合并两个数据框UN_M.49_Countries和UN_M.49_Regions,它们在嵌套列表中包含国家/地区代码。
> UN_M.49_Countries
Code Name ISO_Alpha_3
1 004 Afghanistan AFG
2 248 Åland Islands ALA
3 008 Albania ALB
...
> UN_M.49_Regions
Code Name Parent Children Type
1 001 World 002, 019, 010, 142, 150, 009 Region
2 002 Africa 001 015, 202 Region
3 015 Northern Africa 002 012, 818, 434, 504, 729, 788, 732 Region
...
我想建立一个新表,在UN_M.49_Countries中添加两列。
> new_table
Code Name ISO_Alpha_3 Region Subregion
1 004 Afghanistan AFG Asia Southern Asia
2 248 Åland Islands ALA Europe Northern Europe
3 008 Albania ALB Europe Southern Europe
...
我是编程和R的新手,老实说,我什至不知道从哪里开始。任何帮助将不胜感激!
install.packages("ISOcodes")
library(ISOcodes)
UN_M.49_Countries
UN_M.49_Regions
答案 0 :(得分:0)
如果您需要特定的版本,则可以将南欧更改为所需的任何内容,如果不包含子集,则可以获取整个世界。
查看包装文件。
https://cran.r-project.org/web/packages/ISOcodes/ISOcodes.pdf
data("UN_M.49_Regions")
data("UN_M.49_Countries")
region <- subset(UN_M.49_Regions, Name == "Southern Europe")
codes <- unlist(strsplit(region$Children, ", "))
subset(UN_M.49_Countries, Code %in% codes)
使用tidyverse
library(ISOcodes)
library(tidyverse)
library(stringr)
countries <- UN_M.49_Countries
regions <- UN_M.49_Regions
countries <- UN_M.49_Countries
region_focused <- regions %>%
mutate(codes = str_split(Children,",")) %>%
unnest() %>%
left_join(countries, by = c("codes" = "Code"))
countr_focused <- regions %>%
mutate(codes = str_split(Children,",")) %>%
unnest() %>%
right_join(countries, by = c("codes" = "Code"))