Newer
Older
# Nuts Transformation
#'
#'
#'
#'
#'nuts heatmap output
#' @param group.by a charater vector of regional classification: "REGION" (FADN REGION with 3 numbers),
#' "NUTS1", "NUTS2" or "NUTS3" (A NUTS code begins with 2 letter code referencing the country, as abbr. in
#' the EU's Interinstitutional Style Guide).
#' @param countries a character vector with 3 letter codes of countries:
#' "DEU" for germany, "BEL" for belgium. if "all" is included, all countries are loaded and plotted.
#' @author Yang
#' @describeIn
#' @export
#' @examples
#' nuts.heatmap.group(str_data$info, "NUTS1")
nuts.heatmap.group <- function(fadn.data.info, group.by, countries = "all", onepage = FALSE){
#create DIR>plots ---
ifelse(!dir.exists(paste0(CurrentProjectDirectory,"/plot")),
dir.create(paste0(CurrentProjectDirectory,"/plot")), FALSE)
# dir.create(paste0(CurrentProjectDirectory,"/plot"), FALSE)
dir.create(paste0(CurrentProjectDirectory,"/plot/", "fadn_",
group.by,"_plots"),
if(countries == "all") countries <- unique(fadn.data.info$COUNTRY)
for (country in countries){
heatmap_data <- fadn.data.info %>%
filter(COUNTRY == country) %>%
count(.data[[group.by]],YEAR) %>%
arrange(YEAR) %>%
# pivot_wider(names_from = YEAR,values_from=n) %>%
mutate(across(3:last_col(),function(x)ifelse(is.na(x),0,1)))
# pivot_longer(c(`2004`:`2018` ), names_to = "YEAR", values_to = "n")
heatmap_data <- data.frame(lapply(heatmap_data,as.character))
path_png <- paste0(CurrentProjectDirectory,"/plot/","fadn_",group.by,"_plots/")
if (group.by == "NUTS3" && NROW(heatmap_data$NUTS3 %>% unique()) >100 ) {
text.size = 3
} else{text.size = 11}
p <- heatmap_data %>% ggplot(aes(YEAR, .data[[group.by]], fill= n)) + geom_tile() +
theme() +
ggtitle(country) +
xlab("YEAR") +
ylab(group.by) +
# theme_bw() +
theme(axis.text.y = element_text(size = text.size),legend.position="none")
# multiple plots in one page
p_name <- country
ggsave(plot = p ,
filename = paste0(path_png, country,".png"),
width = 18, height = 8)
assign(p_name, heatmap_data %>% ggplot ( aes(YEAR,.data[[group.by]], fill= n)) +
geom_tile() +
theme(legend.position="none",
axis.text.y = element_text(size = text.size),
axis.text.x = element_text(angle = 45)) +
ggtitle(country))
if(onepage== TRUE) {
png(paste0(path_png,"all_countries.png"), width = 1080, height =1080, units = "px" )
# plots <- list(NED, BEL, BGR, CYP, CZE,DAN,DEU,ELL,ESP,EST,FRA,HUN,IRE,ITA,LTU,LUX,LVA,MLT,OST,POL,POR,ROU,SUO,SVE,SVK,SVN,UKI,HRV)
# mulp <-
multiplot(NED, BEL, BGR, CYP, CZE,DAN,DEU,ELL,ESP,EST,FRA,HUN,IRE,ITA,LTU,LUX,LVA,MLT,OST,POL,POR,ROU,SUO,SVE,SVK,SVN,UKI,HRV, cols=5)
dev.off()}
# ggsave(plot = mulp ,
# filename = paste0(path_png,"all_countries.png"),
# width = 18, height = 8)
multiplot <- function(..., plotlist=NULL, cols) {
require(grid)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
numPlots = length(plots)
# Make the panel
plotCols = cols # Number of columns of plots
plotRows = ceiling(numPlots/plotCols) # Number of rows needed, calculated from # of cols
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(plotRows, plotCols)))
vplayout <- function(x, y)
viewport(layout.pos.row = x, layout.pos.col = y)
# Make each plot, in the correct location
for (i in 1:numPlots) {
curRow = ceiling(i/plotCols)
curCol = (i-1) %% plotCols + 1
print(plots[[i]], vp = vplayout(curRow, curCol ))
}
}
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#' this function related to converting NUTS between different NUTS version in both directions.
#'
#' @param data the data
#' @param countries the three letters code (e.g. "DEU") or "all".
#' If "all" is included, all available countries are loaded.
#' @param NUTS.Year a numeric vector, the year of NUTS (2003,2006,2010,2013,2016).
#' @export
#' @examples
#' ## NOT run:
#' NUTS.convert.all(str_data$info, "DEU", 2016)
#' NUTS.convert.all(str_data$info, "all", 2016)
#' NUTS.convert.all(str_data$info, c("DEU","POL","UKI"), 2016)
#' ## End (NOT run)
NUTS.convert.all<- function(data, countries, NUTS.Year){
# data = test_data
# data = test_data
# countries = c("ELL", special_countries)
# NUTS.Year = 2003
NUTS.Year.eurostat <- c("2003", "2006", "2010", "2013", "2016")
keep_columns_data <- c(colnames(data), "NUTS1_final", "NUTS2_final")
years = unique(data$YEAR)
# selected COUNTRY-YEAR in data
# if (!NUTS.Year %in% years){warning(NUTS.Year, " was not found in your given data")}
if (!NUTS.Year %in% NUTS.Year.eurostat){
warning(NUTS.Year, " was not found in NUTS versions! \nPls give the following NUTS versions:\n2003 2006, 2010, 2013 or 2016.")
return("Please give the following NUTS versions: 2003 2006, 2010, 2013 or 2016")}
if("all"%in%countries) {countries= unique(data$COUNTRY)}
# nuts2
filtered.nuts.trans_left <- nuts2.trans %>%
separate(version, sep="To", c("from", "to"), extra = "drop", fill = "left" ) %>%
filter( to <=NUTS.Year | is.na(to)) %>% filter(COUNTRY %in% countries)
filtered.nuts.trans_right <- nuts2.trans %>%
separate(version, sep="To", c("from", "to"), extra = "drop", fill = "left" ) %>%
filter( to >NUTS.Year ) %>% filter(COUNTRY %in% countries)
# nuts3
filtered.nuts3.trans_left <- nuts3.trans %>%
separate(version, sep="To", c("from", "to"), extra = "drop", fill = "left" ) %>%
filter( to <=NUTS.Year | is.na(to)) %>% filter(COUNTRY %in% countries)
filtered.nuts3.trans_right <- nuts3.trans %>%
separate(version, sep="To", c("from", "to"), extra = "drop", fill = "left" ) %>%
filter( to >NUTS.Year ) %>% filter(COUNTRY %in% countries)
# filtered.countries <- filtered.nuts.trans_left$COUNTRY %>% unique()
special_countries <- c("SVN","HUN","POL","SUO","LTU","IRE","UKI")
data_recoded <- data %>% filter(COUNTRY %in% countries & !COUNTRY %in% special_countries)
if (nrow(data_recoded)!=0){recoded = TRUE}else{recoded = FALSE}
data_rest <- data %>% filter(COUNTRY %in% countries & COUNTRY %in% special_countries)
if (nrow(data_rest)!=0){special = TRUE} else {special = FALSE}
recoded_c <- c(unique(data_recoded$COUNTRY))
# data_rest %>% select(COUNTRY) %>% unique()
# recoded
if(recoded == TRUE){
cat("Countries: ", recoded_c,"\nConverting......\n")
# old to new ......
## as we have at maximum two changes of NUTS regulation, we left join two times
test_data_nuts2 <- data_recoded %>%
left_join(filtered.nuts.trans_left, by = c( "NUTS2", "COUNTRY"))
test_data_nuts2 <- test_data_nuts2 %>%
left_join(filtered.nuts.trans_left, by = c("NUTS2_new"="NUTS2", "COUNTRY"))
## Now we generate the final NUTS2 regulation based on the left joins
recoded_final <- test_data_nuts2 %>%
mutate(NUTS2_final=case_when(
is.na(NUTS2_new) & is.na(NUTS2_new.y) ~ NUTS2,
!is.na(NUTS2_new) & is.na(NUTS2_new.y) ~ NUTS2_new,
TRUE ~ NUTS2_new.y
)) %>% select(-contains(".x"),-contains(".y"), -NUTS2_new)
# new to old ......
new2old_1 <- recoded_final %>%
left_join(filtered.nuts.trans_right, by = c( "NUTS2"= "NUTS2_new", "COUNTRY"))
new2old_2 <- new2old_1 %>%
left_join(filtered.nuts.trans_right, by = c("NUTS2.y"="NUTS2_new", "COUNTRY"))
recoded_final_all <- new2old_2 %>%
mutate(NUTS2_final=case_when(
is.na(NUTS2.y) & is.na(NUTS2.y.y) ~ NUTS2_final,
!is.na(NUTS2.y) & is.na(NUTS2.y.y) ~ NUTS2.y,
TRUE ~ NUTS2.y.y
)) %>% select(-contains(".x"),-contains(".y"), NUTS2.x) %>%
rename(NUTS2=NUTS2.x)
## Now we add new NUTS1 - NUTS1_final
recoded_final_all <- recoded_final_all %>%
mutate(NUTS1_final=str_sub(NUTS2_final,1,3)) %>% select(all_of(keep_columns_data))
cat("finished.\n")
}
if(special == TRUE){
rest_countries <- unique(data_rest$COUNTRY)
for (spec_country in rest_countries){
# filter each special country
assign(paste0("country_",spec_country),data_rest %>% filter(COUNTRY==spec_country))
# cat("converting old to NUTS", NUTS.Year, "......\n")
# old to new
OldToNew = TRUE
if(OldToNew) {
# cat("Converting old version to NUTS", NUTS.Year,"....\n")
if(spec_country == "SVN"){
if (NUTS.Year >=2013){
# 2 changes of NUTS
country_SVN <- country_SVN %>%
left_join(filtered.nuts3.trans_left, by = c( "NUTS3", "COUNTRY")) %>%
left_join(filtered.nuts3.trans_left, by = c( "NUTS3_new"="NUTS3", "COUNTRY")) %>%
mutate(NUTS3_final=case_when(
is.na(NUTS3_new) & is.na(NUTS3_new.y) ~ NUTS3,
!is.na(NUTS3_new) & is.na(NUTS3_new.y) ~ NUTS3_new,
TRUE ~ NUTS3_new.y
)) %>%
mutate( NUTS2_final = str_sub(NUTS3_final,1,4),
NUTS1_final = str_sub(NUTS2_final,1,3)) %>%
select(keep_columns_data)
} else if (NUTS.Year>2003 & NUTS.Year<2013){
# 1 change of NUTS
country_SVN <- country_SVN %>%
left_join(filtered.nuts3.trans_left, by = c( "NUTS3", "COUNTRY")) %>%
mutate(NUTS3_final=case_when(
is.na(NUTS3_new) ~ NUTS3,
TRUE ~ NUTS3_new)) %>%
mutate( NUTS2_final = str_sub(NUTS3_final,1,4),
NUTS1_final = str_sub(NUTS2_final,1,3)) %>%
select(keep_columns_data)
}else{
# no change
country_SVN <- country_SVN %>%
mutate( NUTS2_final = NUTS2,
NUTS1_final = str_sub(NUTS2_final,1,3))%>%
select(keep_columns_data)}
# cat(spec_country,"finished.\n")
}else if(spec_country == "HUN"){
if(NUTS.Year ==2016) {
country_HUN <- country_HUN %>%
mutate( NUTS2_final = NUTS2,
NUTS2_final = if_else(NUTS2_final=="HU10", "HU12", NUTS2_final),
NUTS1_final = str_sub(NUTS2_final,1,3)) %>% select(keep_columns_data)
# cat(spec_country," finished.\n")
}else {
country_HUN <- country_HUN %>%
mutate( NUTS2_final = NUTS2,
NUTS1_final = str_sub(NUTS2_final,1,3)) %>%
select(keep_columns_data)
}
# cat(spec_country,"no change.\n")
}else if(spec_country == "POL") {
pol_nuts2_trans <- nuts2.trans %>% filter(COUNTRY == "POL") %>%
filter(NUTS2!="PL12")
if(NUTS.Year==2016) {
country_POL <- country_POL %>%
left_join(pol_nuts2_trans, by = c( "NUTS2", "COUNTRY")) %>%
mutate(NUTS2_final=case_when(
is.na(NUTS2_new) ~ NUTS2,
TRUE ~ NUTS2_new
)) %>%
mutate(NUTS2_final=if_else(NUTS2_final %in% c("PL12","PL91","PL92"),"PL9192",NUTS2_final),
NUTS1_final = str_sub(NUTS2_final,1,3)) %>% select(keep_columns_data)
} else {
country_POL <- country_POL %>%
mutate( NUTS2_final = NUTS2,
NUTS1_final = str_sub(NUTS2_final,1,3))%>%
select(keep_columns_data)
}
# cat(spec_country," finished.\n")
}else if(spec_country == "SUO") {
if (NUTS.Year >=2010){
country_SUO <- country_SUO %>%
left_join(nuts3.trans, by = c( "NUTS3", "COUNTRY")) %>%
mutate(NUTS3_final=case_when(
is.na(NUTS3_new) ~ NUTS3,
TRUE ~ NUTS3_new
)) %>%
mutate( NUTS2_final = str_sub(NUTS3_final,1,4),
NUTS1_final = str_sub(NUTS2_final,1,3)) %>%
select(keep_columns_data)
} else{
country_SUO <- country_SUO %>%
mutate( NUTS2_final = NUTS2,
NUTS1_final = str_sub(NUTS2_final,1,3)) %>%
select(keep_columns_data)
}
# cat(spec_country," old to new finished.\n")
}else if(spec_country == "LTU") {
if(NUTS.Year>=2016){
country_LTU <- country_LTU %>%
left_join(nuts3.trans, by = c( "NUTS3", "COUNTRY")) %>%
mutate(NUTS3_final=case_when(
is.na(NUTS3_new) ~ NUTS3,
TRUE ~ NUTS3_new
)) %>%
mutate( NUTS2_final = str_sub(NUTS3_final,1,4),
NUTS1_final = str_sub(NUTS2_final,1,3)) %>%
select(keep_columns_data)
}else {
country_LTU <- country_LTU %>%
mutate( NUTS2_final = NUTS2,
NUTS1_final = str_sub(NUTS2_final,1,3)) %>%
select(keep_columns_data)
}
# cat(spec_country," finished.\n")
}else if(spec_country == "IRE") {
if(NUTS.Year>=2016) {
country_IRE <- country_IRE %>%
mutate(NUTS3=if_else(NUTS3=="IE02","IE022",NUTS3)) %>%
left_join(nuts3.trans, by = c( "NUTS3", "COUNTRY")) %>%
mutate(NUTS3_final=case_when(
is.na(NUTS3_new) ~ NUTS3,
TRUE ~ NUTS3_new
)) %>%
mutate( NUTS2_final = str_sub(NUTS3_final,1,4),
NUTS1_final = str_sub(NUTS2_final,1,3)) %>%
select(keep_columns_data)
}else {
country_IRE <- country_IRE %>%
mutate( NUTS2_final = NUTS2,
NUTS1_final = str_sub(NUTS2_final,1,3)) %>%
select(keep_columns_data)
}
# cat(spec_country," finished.\n")
}else if(spec_country == "UKI") {
if(NUTS.Year == 2016 ) {
# print("2016")
country_UKI_UKM <- country_UKI %>% filter(grepl("UKM",NUTS2)) %>%
left_join(nuts3.trans, by = c("NUTS3", "COUNTRY")) %>%
mutate(NUTS3_final=case_when(
is.na(NUTS3_new) ~ NUTS3,
TRUE ~ NUTS3_new
),
NUTS3_final=case_when(
NUTS3=="UKM2" ~ "UKM7",
NUTS3=="UKM3" ~ "UKM9",
TRUE ~ NUTS3_final
)) %>%
mutate( NUTS2_final = str_sub(NUTS3_final,1,4),
NUTS1_final = str_sub(NUTS2_final,1,3)) %>%
select(keep_columns_data)
# 2010: rest
country_UKI_rest <- country_UKI %>% filter(!grepl("UKM",NUTS2)) %>%
left_join(filtered.nuts.trans_left %>% filter(grepl("UKN|UKD",NUTS2)), by = c( "NUTS2", "COUNTRY")) %>%
mutate(NUTS2_final=case_when(
is.na(NUTS2_new) ~ NUTS2,
TRUE ~ NUTS2_new
)) %>%
mutate( NUTS1_final = str_sub(NUTS2_final,1,3)) %>%
select(keep_columns_data)
country_UKI <- country_UKI_rest %>% bind_rows(country_UKI_UKM)
}else if(NUTS.Year <= 2013 & NUTS.Year>=2010){
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
country_UKI_rest <- country_UKI %>% filter(!grepl("UKM",NUTS2)) %>%
left_join(filtered.nuts.trans_left %>% filter(grepl("UKN|UKD",NUTS2)), by = c( "NUTS2", "COUNTRY")) %>%
mutate(NUTS2_final=case_when(
is.na(NUTS2_new) ~ NUTS2,
TRUE ~ NUTS2_new
)) %>%
mutate( NUTS1_final = str_sub(NUTS2_final,1,3)) %>%
select(keep_columns_data)
country_UKI <- country_UKI_rest %>% bind_rows(country_UKI %>% filter(grepl("UKM",NUTS2)))
}else{
country_UKI <- country_UKI %>%
mutate( NUTS2_final = NUTS2,
NUTS1_final = str_sub(NUTS2_final,1,3)) %>%
select(keep_columns_data)
}
# cat(spec_country," finished.\n")}
}
# new to old
NewtoOld = TRUE
# if(nrow(new2old_spe) !=0) {
if(NewtoOld) {
# cat("Converting old version to NUTS", NUTS.Year,"....\n")
if(spec_country == "SVN"){
if (NUTS.Year == 2003){
# 2 changes to latest version
# country_SVN %>%
# mutate(NUTS2_final = case_when(
# NUTS2_final == "SI03" ~ "SI01",
# NUTS2_final == "SI04" ~ "SI02",
# TRUE ~ NUTS2_final
# ))
country_SVN <- country_SVN %>%
left_join(filtered.nuts3.trans_right, by = c( "NUTS3" = "NUTS3_new", "COUNTRY")) %>%
left_join(filtered.nuts3.trans_right, by = c( "NUTS3.y"="NUTS3_new", "COUNTRY")) %>%
mutate(NUTS3_final=case_when(
is.na(NUTS3.y) & is.na(NUTS3.y.y) ~ NUTS2_final,
!is.na(NUTS3.y) & is.na(NUTS3.y.y) ~ NUTS3.y,
TRUE ~ NUTS3.y.y
)) %>%
mutate( NUTS2_final = if_else(nchar(NUTS3_final)==4,NUTS3_final,str_sub(NUTS3_final,1,4)),
NUTS1_final = str_sub(NUTS2_final,1,3)) %>% rename(NUTS3 = NUTS3.x) %>%
select(keep_columns_data)}
# select(-contains(".x"),-contains(".y"), NUTS3.x, -NUTS3_final) %>%}
if(NUTS.Year <2013 & NUTS.Year>2003){
# 06 and 10
# 1 change to latest version
country_SVN <- country_SVN %>%
mutate(NUTS2_final = case_when(
NUTS2_final == "SI03" ~ "SI01",
NUTS2_final == "SI04" ~ "SI02",
TRUE ~ NUTS2_final
)) %>%
select(keep_columns_data)
}
# cat(spec_country," finished.\n")
}else if(spec_country == "HUN"){
if(NUTS.Year !=2016) {
country_HUN <- country_HUN %>%
mutate( NUTS2_final = NUTS2,
NUTS2_final = if_else(NUTS2_final=="HU12", "HU10", NUTS2_final),
NUTS1_final = str_sub(NUTS2_final,1,3)) %>% select(keep_columns_data)
}
# cat(spec_country," finished.\n")
}else if(spec_country == "POL") {
pol_nuts2_trans <- nuts2.trans %>% filter(COUNTRY == "POL")
if(NUTS.Year!=2016) {
country_POL <- country_POL %>%
left_join(pol_nuts2_trans, by = c("NUTS2"="NUTS2_new", "COUNTRY")) %>%
mutate(NUTS2_final=case_when(
is.na(NUTS2.y) ~ NUTS2_final,
TRUE ~ NUTS2.y
)) %>%
mutate(NUTS1_final = str_sub(NUTS2_final,1,3)) %>% select(keep_columns_data)
}
# cat(spec_country," finished.\n")
}else if(spec_country == "SUO") {
if (NUTS.Year <2006){
# 1: recoded using nuts2.trans
# 2: merge(reverse) using nuts3.trans
country_SUO <- country_SUO %>%
left_join(nuts3.trans %>% filter(grepl("FI1D",NUTS3_new)), by = c( "NUTS3" = "NUTS3_new", "COUNTRY")) %>%
mutate(NUTS3_final=case_when(
is.na(NUTS3.y) ~ NUTS2_final,
TRUE ~ NUTS3.y
)) %>%
mutate( NUTS2_final = str_sub(NUTS3_final,1,4),
NUTS1_final = str_sub(NUTS2_final,1,3)) %>%
select(keep_columns_data) %>%
mutate(NUTS2_final = if_else(NUTS2_final %in% c("FI1B", "FI1C"),"FI18",NUTS2_final ),
NUTS2_final = if_else(NUTS2_final =="","FI19",NUTS2_final ))
}
# cat(spec_country," new to old finished.\n")
}else if(spec_country == "LTU") {
if(NUTS.Year<2016){
country_LTU <- country_LTU %>%
left_join(nuts3.trans, by = c( "NUTS3" = "NUTS3_new", "COUNTRY")) %>%
mutate(NUTS3_final=case_when(
is.na(NUTS3.y) ~ NUTS3,
TRUE ~ NUTS3.y
)) %>%
mutate( NUTS2_final = str_sub(NUTS3_final,1,4),
NUTS1_final = str_sub(NUTS2_final,1,3)) %>%
select(keep_columns_data)
}
# cat(spec_country," finished.\n")
}else if(spec_country == "IRE") {
if(NUTS.Year<2016) {
country_IRE <- country_IRE %>%
mutate(NUTS3=if_else(NUTS3=="IE02","IE022",NUTS3),
NUTS3=if_else(NUTS3=="IE06","IE062",NUTS3)) %>%
left_join(nuts3.trans, by = c( "NUTS3" = "NUTS3_new", "COUNTRY")) %>%
mutate(NUTS3_final=case_when(
is.na(NUTS3.y) ~ NUTS3,
TRUE ~ NUTS3.y
)) %>%
mutate( NUTS2_final = str_sub(NUTS3_final,1,4),
NUTS1_final = str_sub(NUTS2_final,1,3)) %>%
select(keep_columns_data)
}
# cat(spec_country," finished.\n")
}else if(spec_country == "UKI") {
if(NUTS.Year <2010 ) {
country_UKI_UKM <- country_UKI %>% filter(grepl("UKM",NUTS2)) %>%
left_join(nuts3.trans, by = c("NUTS3"= "NUTS3_new", "COUNTRY")) %>%
mutate(NUTS3_final=case_when(
is.na(NUTS3.y) ~ NUTS3,
TRUE ~ NUTS3.y),
NUTS3_final=case_when(
NUTS3=="UKM7" ~ "UKM2",
NUTS3=="UKM9" ~ "UKM3",
TRUE ~ NUTS3_final
)) %>%
mutate( NUTS2_final = str_sub(NUTS3_final,1,4),
NUTS1_final = str_sub(NUTS2_final,1,3)) %>%
select(keep_columns_data)
# 2010: rest
country_UKI_rest <- country_UKI %>%
filter(!grepl("UKM",NUTS2)) %>%
left_join(nuts2.trans %>%
filter(grepl("UKN|UKD",NUTS2)), by = c( "NUTS2" = "NUTS2_new", "COUNTRY")) %>%
mutate(NUTS2_final=case_when(
is.na(NUTS2.y) ~ NUTS2_final,
TRUE ~ NUTS2.y
)) %>%
mutate( NUTS1_final = str_sub(NUTS2_final,1,3),
NUTS2_final = if_else(NUTS2_final == "UKN1", "UKN", NUTS2_final)) %>%
select(keep_columns_data)
country_UKI <- country_UKI_rest %>% bind_rows(country_UKI_UKM)
}
if(NUTS.Year < 2013 & NUTS.Year>2010){
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
country_UKI_rest <- country_UKI %>% filter(!grepl("UKM",NUTS2)) %>%
left_join(nuts2.trans %>%
filter(grepl("UKN|UKD",NUTS2)), by = c( "NUTS2" = "NUTS2_new", "COUNTRY")) %>%
mutate(NUTS2_final=case_when(
is.na(NUTS2.y) ~ NUTS2_final,
TRUE ~ NUTS2.y
)) %>%
mutate( NUTS1_final = str_sub(NUTS2_final,1,3),
NUTS2_final = if_else(NUTS2_final == "UKN1", "UKN", NUTS2_final)) %>%
select(keep_columns_data)
country_UKI <- country_UKI_rest %>% bind_rows(country_UKI %>% filter(grepl("UKM",NUTS2)))
}
# cat(spec_country," finished.\n")
}
}
}
}
list <- c(paste0("country_",rest_countries))
n_sp <- length(list)
spe.final = list()
for (i in 1:n_sp) {spe.final <- spe.final %>%
bind_rows(eval(parse(text = list[[i]])))}
}
cat ("finished!\n")
if(recoded & special){
final_data <- spe.final %>% bind_rows(recoded_final_all)
}else if(recoded){final_data <- recoded_final_all
}else if(special){final_data <- spe.final}
return(final_data)
}