-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImport latest country classifications.R
More file actions
251 lines (191 loc) · 7.7 KB
/
Import latest country classifications.R
File metadata and controls
251 lines (191 loc) · 7.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# Import latest country classifications
# Alice Lepissier
# alice.lepissier@gmail.com
# Last update April 2023
## ## ## ## ## ## ## ## ## ## ##
# INDEX ####
## ## ## ## ## ## ## ## ## ## ##
# Preamble
# Import existing sheets
# Merge latest UNSD groups
# Merge latest HDI groups
# Merge latest World Bank groups
# Latest developments
# Create workbook
## ## ## ## ## ## ## ## ## ## ##
# PREAMBLE ####
## ## ## ## ## ## ## ## ## ## ##
library(here)
setwd(here())
library(openxlsx)
library(readxl)
library(tidyverse)
## ## ## ## ## ## ## ## ## ## ##
# IMPORT EXISTING SHEETS ####
## ## ## ## ## ## ## ## ## ## ##
codes <- read_xlsx("Codes_Crosswalk.xlsx", sheet = "Codes")
regional <- read_xlsx("Codes_Crosswalk.xlsx",
sheet = "UN_Regional_Groupings")
wb <- read_xlsx("Codes_Crosswalk.xlsx",
sheet = "World_Bank_Groupings")
## ## ## ## ## ## ## ## ## ## ##
# MERGE LATEST UNSD GROUPS ####
## ## ## ## ## ## ## ## ## ## ##
# Source: https://unstats.un.org/unsd/methodology/m49/
# 2023 classification
UNSD <- read_xlsx("2023_UNSD.xlsx", sheet = "Sheet1") %>%
select(Country = "Country or Area",
ISO3166.3 = "ISO-alpha3 Code",
New_UN_LDC = "Least Developed Countries (LDC)",
New_UN_LLDC = "Land Locked Developing Countries (LLDC)",
New_UN_SIDS = "Small Island Developing States (SIDS)") %>%
mutate_at(c("New_UN_LDC", "New_UN_LLDC", "New_UN_SIDS"),
~ifelse(. == "x", 1, .)) %>%
mutate_at(c("New_UN_LDC", "New_UN_LLDC", "New_UN_SIDS"),
~ifelse(is.na(.), 0, .))
# As of 2021, UNSD removed the Developed/Developing classification from M49
# Download May 2022 classification by UNSD
dev <- read_xlsx("2022_UNSD_Developing-Developed.xlsx", sheet = "Distinction as of May 2022") %>%
select(ISO3166.3 = "ISO-alpha3 Code",
`New_UN_Developing-Developed` = `Developed / Developing regions`)
UNSD <- left_join(UNSD, dev,
by = c("ISO3166.3"))
# Check countries with no group
UNSD %>% filter(is.na(`New_UN_Developing-Developed`))
codes <- left_join(codes, UNSD %>%
select(-Country),
by = c("ISO3166.3"))
# Check which countries have changed classification
codes %>%
select(Country,
UN_LDC, New_UN_LDC) %>%
filter(UN_LDC != New_UN_LDC) %>%
arrange(Country)
codes %>%
select(Country,
UN_LLDC, New_UN_LLDC) %>%
filter(UN_LLDC != New_UN_LLDC) %>%
arrange(Country)
codes %>%
select(Country,
UN_SIDS, New_UN_SIDS) %>%
filter(UN_SIDS != New_UN_SIDS) %>%
arrange(Country)
codes %>%
select(Country,
`UN_Developing-Developed`, `New_UN_Developing-Developed`) %>%
filter(`UN_Developing-Developed` != `New_UN_Developing-Developed`) %>%
arrange(Country)
codes <- codes %>%
select(-c(UN_LDC, UN_LLDC, UN_SIDS, `UN_Developing-Developed`))
colnames(codes) <- gsub("New_", "", colnames(codes))
## ## ## ## ## ## ## ## ## ## ##
# MERGE LATEST HDI GROUPS ####
## ## ## ## ## ## ## ## ## ## ##
# Source: https://hdr.undp.org/data-center/human-development-index#/indicies/HDI
# 2022 classification
UNDP <- read_xlsx("2022_UNDP.xlsx", sheet = "Table 1", skip = 6) %>%
select(Country = ...2,
HDI = `2021...3`) %>%
mutate(HDI = as.numeric(HDI)) %>%
filter(!is.na(HDI))
UNDP <- UNDP %>%
mutate(New_UNDP_HDI_Group = case_when(HDI < 0.550 ~ "Lo HDI",
HDI >= 0.550 & HDI <= 0.699 ~ "Med HDI",
HDI >= 0.700 & HDI <= 0.799 ~ "Hi HDI",
HDI >= 0.800 ~ "VHi HDI"))
# Check countries with no group
UNDP %>% filter(is.na(New_UNDP_HDI_Group))
UNDP <- left_join(UNDP, codes %>%
select(Country, ISO3166.3),
by = c("Country"))
UNDP %>% filter(is.na(ISO3166.3))
codes <- left_join(codes, UNDP %>%
select(ISO3166.3, New_UNDP_HDI_Group) %>%
filter(!is.na(ISO3166.3)),
by = c("ISO3166.3"))
# Check which countries have changed classification
codes %>%
select(Country, UNDP_HDI_Group, New_UNDP_HDI_Group) %>%
filter(UNDP_HDI_Group != New_UNDP_HDI_Group) %>%
arrange(Country)
codes <- codes %>%
select(-UNDP_HDI_Group) %>%
rename(UNDP_HDI_Group = New_UNDP_HDI_Group)
## ## ## ## ## ## ## ## ## ## ## ## ##
# MERGE LATEST WORLD BANK GROUPS ####
## ## ## ## ## ## ## ## ## ## ## ## ##
# Source: https://datahelpdesk.worldbank.org/knowledgebase/articles/906519-world-bank-country-and-lending-groups
# 2022 classification
WB <- read_excel("2022_World Bank.xlsx") %>%
select(Country = Economy,
ISO3166.3 = Code,
New_WB_Income_Group = "Income group",
New_WB_Lending_Category = "Lending category",
New_WB_Other = "Other (EMU or HIPC)",
Region) %>%
drop_na(Region)
WB <- WB %>%
mutate(New_WB_Income_Group = case_when(New_WB_Income_Group == "Low income" ~ "LIC",
New_WB_Income_Group == "Lower middle income" ~ "LMC",
New_WB_Income_Group == "Upper middle income" ~ "UMC",
New_WB_Income_Group == "High income" ~ "HIC"))
# Check countries with no group
WB %>% filter(is.na(New_WB_Income_Group))
# Venezuela has been temporarily unclassified by World Bank
# Assign latest known classification
WB <- WB %>% mutate(New_WB_Income_Group = ifelse(ISO3166.3 == "VEN", "UMC", New_WB_Income_Group))
codes <- left_join(codes,
WB %>%
select(ISO3166.3, New_WB_Income_Group, New_WB_Lending_Category, New_WB_Other),
by = c("ISO3166.3"))
# Countries that have changed income group
codes %>%
select(Country, WB_Income_Group, New_WB_Income_Group) %>%
filter(WB_Income_Group != New_WB_Income_Group) %>%
arrange(Country) %>%
distinct(Country, .keep_all = TRUE)
# Countries that have changed lending category
codes %>%
select(Country, WB_Lending_Category, New_WB_Lending_Category) %>%
filter(WB_Lending_Category != New_WB_Lending_Category) %>%
arrange(Country) %>%
distinct(Country, .keep_all = TRUE)
# Countries that changed other category
codes %>%
select(Country, WB_Other, New_WB_Other) %>%
filter(WB_Other != New_WB_Other) %>%
arrange(Country) %>%
distinct(Country, .keep_all = TRUE)
codes <- codes %>%
select(-c(WB_Income_Group, WB_Lending_Category, WB_Other))
colnames(codes) <- gsub("New_", "", colnames(codes))
## ## ## ## ## ## ## ## ## ## ## ## ##
# LATEST DEVELOPMENTS ####
## ## ## ## ## ## ## ## ## ## ## ## ##
# Costa Rica joined OECD in May 2021
codes <- codes %>%
mutate(OECD = ifelse(ISO3166.3 == "CRI", 1, OECD))
## ## ## ## ## ## ## ## ## ## ##
# CREATE WORKBOOK ####
## ## ## ## ## ## ## ## ## ## ##
codes <- codes %>%
select(ISO3166.3, ISO3166.2, Country,
UN_Region, UN_Region_Code, `UN_Sub-region`, `UN_Sub-region_Code`, UN_Intermediate_Region, UN_Intermediate_Region_Code,
UN_M49_Code, UN_LDC, UN_LLDC, UN_SIDS, `UN_Developing-Developed`, UNDP_HDI_Group,
WB_Income_Group, WB_Region, WB_Lending_Category, WB_Other,
IMF_Code, OECD, EU27, `Arab League`, Centroid_Longitude, Centroid_Latitude)
codes <- codes %>%
arrange(ISO3166.3)
book <- createWorkbook()
hs <- createStyle(textDecoration = "Bold")
addWorksheet(book, "Codes")
writeData(book, sheet = "Codes",
x = codes, headerStyle = hs)
addWorksheet(book, "UN_Regional_Groupings")
writeData(book, sheet = "UN_Regional_Groupings",
x = regional, headerStyle = hs)
addWorksheet(book, "World_Bank_Groupings")
writeData(book, sheet = "World_Bank_Groupings",
x = wb, headerStyle = hs)
saveWorkbook(book, "Codes_Crosswalk.xlsx", overwrite = TRUE)