GitLab at IIASA

nuts_converter.R 23.4 KiB
Newer Older
# Nuts Transformation
#'
#'
#'
#'
#' nuts heatmap output
Xinxin Yang's avatar
Xinxin Yang committed
#' @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).
Xinxin Yang's avatar
Xinxin Yang committed
#' @param fadn.data.info fadn info data
#' @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
Xinxin Yang's avatar
Xinxin Yang committed
#' ## NOT run:
#' nuts.heatmap.group(str_data$info, "NUTS1")
Xinxin Yang's avatar
Xinxin Yang committed
#' ## End (NOT run)
nuts.heatmap.group <- function(fadn.data.info, group.by, countries = "all", onepage = FALSE){
  #create DIR>plots ---
Xinxin Yang's avatar
Xinxin Yang committed
  ifelse(!dir.exists(paste0(CurrentProjectDirectory,"/plot")),
         dir.create(paste0(CurrentProjectDirectory,"/plot")), FALSE)
Xinxin Yang's avatar
Xinxin Yang committed
  # dir.create(paste0(CurrentProjectDirectory,"/plot"), FALSE)
  # create group folder ---
  dir.create(paste0(CurrentProjectDirectory,"/plot/", "fadn_",
                    group.by,"_plots"),
Xinxin Yang's avatar
Xinxin Yang committed
             showWarnings = FALSE)
  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 ))
  }



}
#' this function related to converting NUTS between different NUTS version in both directions.
#'
#' @param data FADN 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("Non-special country 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){
Xinxin Yang's avatar
Xinxin Yang committed
            # print("2013 & 2010")

            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){
Xinxin Yang's avatar
Xinxin Yang committed
            # print("2013 & 2010")

            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("Sepcial country finished!\n")



    if(recoded & special){
      final_data <- spe.final %>% bind_rows(recoded_final_all)
      cat ("Combination special and non-countries finished!\n")
    }else if(recoded){
      final_data <- recoded_final_all
    }else if(special){
      final_data <- spe.final

      }