##############################
# countries for green pillar
#
#############################
# library(haven)
# library(writexl)
# library(dplyr)
# library(countrycode)
# library(openxlsx)
# library(questionr)
# library(sjPlot)
 library(labelled)
 library(stringr)
# library(purrr)


# ---- Function to rename all countries ----


# Create a function to recode the country samples:

# country abbreviations follow ISO codes 3166-1 alpha 2 as shown here:
# https://www.iso.org/obp/ui/#search
recoded_country <- function(df, country_variable) {
  df %>%
    mutate(country_new = case_when(
      str_detect(df[[country_variable]], "AL|29") ~ "AL",
      str_detect(df[[country_variable]], "AM") ~ "AM",
      str_detect(df[[country_variable]], "AR|AR-Argentina") ~ "AR",
      str_detect(df[[country_variable]], "AUS|AU|AUS-Australia|Australia") ~ "AU",
      str_detect(df[[country_variable]], "AT|AT-Austria|A-Austria|^1$") ~ "AT",
      str_detect(df[[country_variable]], "AZ") ~ "AZ",
      str_detect(df[[country_variable]], "BA") ~ "BA",
      str_detect(df[[country_variable]], "^BE$|BE-Belgium|^2$") ~ "BE",
      str_detect(df[[country_variable]], "BG|BG-Bulgaria|^3$") ~ "BG",
      str_detect(df[[country_variable]], "BY") ~ "BY",
      str_detect(df[[country_variable]], "CA|CA-Canada|CDN-Canada|Canada - CDN") ~ "CA",
      str_detect(df[[country_variable]], "CH|CH-Switzerland") ~ "CH",
      str_detect(df[[country_variable]], "CL|CL-Chile|RCH-Chile") ~ "CL",
      str_detect(df[[country_variable]], "CN|CN-China") ~ "CN",
      str_detect(df[[country_variable]], "^CY_TCC$|^CY$|^4$|^CY-TCC$") ~ "CY",
      str_detect(df[[country_variable]], "CZ|CZ-Czech Republic|CR-Czech Republic|Czech Republic - CR|^5$") ~ "CZ",
      str_detect(df[[country_variable]], "DE|DE-Germany|^6$|D-E|Germany-East|DE-E|D-W|Germany-West|DE-W|^DE_E$|^DE_W$") ~ "DE",
      str_detect(df[[country_variable]], "DK|DK-Denmark|^7$") ~ "DK",
      str_detect(df[[country_variable]], "EE|^8$") ~ "EE",
      str_detect(df[[country_variable]], "ES|ES-Spain|E-Spain|Spain - E|10") ~ "ES",
      str_detect(df[[country_variable]], "FI|FI-Finland|SF-Finland|11") ~ "FI",
      str_detect(df[[country_variable]], "FR|FR-France|12") ~ "FR",
      str_detect(df[[country_variable]], "^GB$|GB-Great Britain|GB-GBN|Great Britain") ~ "GB_GBN",
      str_detect(df[[country_variable]], "28") ~ "GB_UK",
      str_detect(df[[country_variable]], "GE") ~ "GE",
      str_detect(df[[country_variable]], "GR|GR-Greece|Greece - GR|^9$") ~ "GR",
      str_detect(df[[country_variable]], "HR|HR-Croatia|13") ~ "HR",
      str_detect(df[[country_variable]], "HU|HU-Hungary|H-Hungary|Hungary - H|14") ~ "HU",
      str_detect(df[[country_variable]], "IE|IRL-Ireland|Ireland - IRL|15") ~ "IE",
      str_detect(df[[country_variable]], "IL|IL-Israel") ~ "IL",
      str_detect(df[[country_variable]], "IN|IN-India") ~ "IN",
      str_detect(df[[country_variable]], "IS|IS-Iceland|30") ~ "IS",
      str_detect(df[[country_variable]], "IT|IT-Italy|I-Italy|Italy - I|16") ~ "IT",
      str_detect(df[[country_variable]], "JP|JP-Japan|J-Japan|Japan - J") ~ "JP",
      str_detect(df[[country_variable]], "KR|KR-Korea") ~ "KR",
      str_detect(df[[country_variable]], "LU|LU-Luxembourg|Luxembourg - LU|18") ~ "LU",
      str_detect(df[[country_variable]], "LV|LV-Latvia|19") ~ "LV",
      str_detect(df[[country_variable]], "LT|LT-Lithuania|17") ~ "LT",
      str_detect(df[[country_variable]], "ME|32") ~ "ME",
      str_detect(df[[country_variable]], "MK|FYR of Macedonia|33") ~ "MK",
      str_detect(df[[country_variable]], "MD") ~ "MD",
      str_detect(df[[country_variable]], "MX|MX-Mexico|MEX-Mexico") ~ "MX",
      str_detect(df[[country_variable]], "MT|20") ~ "MT",
      str_detect(df[[country_variable]], "NL|NL-Netherlands|21") ~ "NL",
      str_detect(df[[country_variable]], "NO|NO-Norway|N-Norway|Norway - N|34") ~ "NO",
      str_detect(df[[country_variable]], "NIRL|NIRL-Northern Ireland|GB-NIR") ~ "GB_NIR",
      str_detect(df[[country_variable]], "NZ|NZ-New Zealand") ~ "NZ",
      str_detect(df[[country_variable]], "PH|PH-Philippines|RP-Philippines|Philippines - RP") ~ "PH",
      str_detect(df[[country_variable]], "PL|PL-Poland|Poland - PL|22") ~ "PL",
      str_detect(df[[country_variable]], "PT|PT-Portugal|P-Portugal|23") ~ "PT",
      str_detect(df[[country_variable]], "RO|24") ~ "RO",
      str_detect(df[[country_variable]], "RS|35") ~ "RS",
      str_detect(df[[country_variable]], "RU|RU-Russia|RUS-Russia") ~ "RU",
      str_detect(df[[country_variable]], "SE|SE-Sweden|S-Sweden|25") ~ "SE",
      str_detect(df[[country_variable]], "SI|SI-Slovenia|SLO-Slovenia|Slovenia - SLO|26") ~ "SI",
      str_detect(df[[country_variable]], "SK|SK-Slovakia|27") ~ "SK",
      str_detect(df[[country_variable]], "TH|TH-Thailand") ~ "TH",
      str_detect(df[[country_variable]], "TR|TR-Turkey|36") ~ "TR",
      str_detect(df[[country_variable]], "TW") ~ "TW",
      str_detect(df[[country_variable]], "UA") ~ "UA",
      str_detect(df[[country_variable]], "US|US-United States|USA-United States") ~ "US",
      str_detect(df[[country_variable]], "XK|31") ~ "XK",
      str_detect(df[[country_variable]], "ZA|ZA-South Africa") ~ "ZA",
      str_detect(df[[country_variable]], "^BE-FLA$") ~ "BE_FLA",
      TRUE ~ df[[country_variable]]  
    ),
    country_lng = case_when(
      str_detect(df[[country_variable]], "AL|29") ~ "Albania",
      str_detect(df[[country_variable]], "AM") ~ "Armenia",
      str_detect(df[[country_variable]], "AR|AR-Argentina") ~ "Argentina",
      str_detect(df[[country_variable]], "AUS|AU|AUS-Australia|Australia") ~ "Australia",
      str_detect(df[[country_variable]], "AT|AT-Austria|A-Austria|^1$") ~ "Austria",
      str_detect(df[[country_variable]], "AZ") ~ "Azerbaijan",
      str_detect(df[[country_variable]], "BA") ~ "Bosnia and Herzegovina",
      str_detect(df[[country_variable]], "^BE-FLA$") ~ "Belgium Flanders",
      str_detect(df[[country_variable]], "^BE$|BE-Belgium|^2$") ~ "Belgium",
      str_detect(df[[country_variable]], "BG|BG-Bulgaria|^3$") ~ "Bulgaria",
      str_detect(df[[country_variable]], "BY") ~ "Belarus",
      str_detect(df[[country_variable]], "CA|CA-Canada|CDN-Canada|Canada - CDN") ~ "Canada",
      str_detect(df[[country_variable]], "CH|CH-Switzerland") ~ "Switzerland",
      str_detect(df[[country_variable]], "CL|CL-Chile|RCH-Chile") ~ "Chile",
      str_detect(df[[country_variable]], "CN|CN-China") ~ "China",
      str_detect(df[[country_variable]], "^CY_TCC$|^CY$|^4$") ~ "Cyprus",
      str_detect(df[[country_variable]], "CZ|CZ-Czech Republic|CR-Czech Republic|Czech Republic - CR|^5$") ~ "Czech Republic",
      str_detect(df[[country_variable]], "DE|DE-Germany|^6$|D-E|Germany-East|DE-E|D-W|Germany-West|DE-W") ~ "Germany",
      str_detect(df[[country_variable]], "^DK$|DK-Denmark|^7$") ~ "Denmark",
      str_detect(df[[country_variable]], "EE|^8$") ~ "Estonia",
      str_detect(df[[country_variable]], "ES|ES-Spain|E-Spain|Spain - E|^10$") ~ "Spain",
      str_detect(df[[country_variable]], "FI|FI-Finland|SF-Finland|11") ~ "Finland",
      str_detect(df[[country_variable]], "FR|FR-France|12") ~ "France",
      str_detect(df[[country_variable]], "GB|GB-Great Britain|GB-GBN|Great Britain|28") ~ "Great Britain",
      str_detect(df[[country_variable]], "GE") ~ "Georgia",
      str_detect(df[[country_variable]], "GR|GR-Greece|Greece - GR|^9$") ~ "Greece",
      str_detect(df[[country_variable]], "HR|HR-Croatia|13") ~ "Croatia",
      str_detect(df[[country_variable]], "HU|HU-Hungary|H-Hungary|Hungary - H|14") ~ "Hungary",
      str_detect(df[[country_variable]], "IE|IRL-Ireland|Ireland - IRL|15") ~ "Ireland",
      str_detect(df[[country_variable]], "IL|IL-Israel") ~ "Israel",
      str_detect(df[[country_variable]], "IN|IN-India") ~ "India",
      str_detect(df[[country_variable]], "IS|IS-Iceland|30") ~ "Iceland",
      str_detect(df[[country_variable]], "IT|IT-Italy|I-Italy|Italy - I|16") ~ "Italy",
      str_detect(df[[country_variable]], "JP|JP-Japan|J-Japan|Japan - J") ~ "Japan",
      str_detect(df[[country_variable]], "KR|KR-Korea") ~ "Korea",
      str_detect(df[[country_variable]], "LU|LU-Luxembourg|Luxembourg - LU|18") ~ "Luxembourg",
      str_detect(df[[country_variable]], "LV|LV-Latvia|19") ~ "Latvia",
      str_detect(df[[country_variable]], "LT|LT-Lithuania|17") ~ "Lithuania",
      str_detect(df[[country_variable]], "^ME$|^32$") ~ "Montenegro",
      str_detect(df[[country_variable]], "MK|FYR of Macedonia|33") ~ "North Macedonia",
      str_detect(df[[country_variable]], "MD") ~ "Moldova",
      str_detect(df[[country_variable]], "MX|MX-Mexico|MEX-Mexico") ~ "Mexico",
      str_detect(df[[country_variable]], "MT|20") ~ "Malta",
      str_detect(df[[country_variable]], "NL|NL-Netherlands|21") ~ "Netherlands",
      str_detect(df[[country_variable]], "NO|NO-Norway|N-Norway|Norway - N|34") ~ "Norway",
      str_detect(df[[country_variable]], "GB-NIR|NIRL|NIRL-Northern Ireland") ~ "Northern Ireland",
      str_detect(df[[country_variable]], "NZ|NZ-New Zealand") ~ "New Zealand",
      str_detect(df[[country_variable]], "PH|PH-Philippines|RP-Philippines|Philippines - RP") ~ "Philippines",
      str_detect(df[[country_variable]], "PL|PL-Poland|Poland - PL|22") ~ "Poland",
      str_detect(df[[country_variable]], "PT|PT-Portugal|P-Portugal|23") ~ "Portugal",
      str_detect(df[[country_variable]], "RO|24") ~ "Romania",
      str_detect(df[[country_variable]], "RS|35") ~ "Serbia",
      str_detect(df[[country_variable]], "RU|RU-Russia|RUS-Russia") ~ "Russia",
      str_detect(df[[country_variable]], "SE|SE-Sweden|S-Sweden|25") ~ "Sweden",
      str_detect(df[[country_variable]], "SI|SI-Slovenia|SLO-Slovenia|Slovenia - SLO|26") ~ "Slovenia",
      str_detect(df[[country_variable]], "SK|SK-Slovakia|27") ~ "Slovakia",
      str_detect(df[[country_variable]], "TH|TH-Thailand") ~ "Thailand",
      str_detect(df[[country_variable]], "TR|TR-Turkey|36") ~ "Turkey",
      str_detect(df[[country_variable]], "TW") ~ "Taiwan",
      str_detect(df[[country_variable]], "UA") ~ "Ukraine",
      str_detect(df[[country_variable]], "US|US-United States|USA-United States") ~ "United States",
      str_detect(df[[country_variable]], "ZA|ZA-South Africa") ~ "South Africa",
      str_detect(df[[country_variable]], "XK|31") ~ "Kosovo",
      TRUE ~ df[[country_variable]]
      )
     )
    }

 
# func_country <- function(df, country_variable) {
#   df %>%
#     mutate(country = case_when(
#       # Dynamically apply all country patterns using the list
#       str_detect(df[[country_variable]], "AL|29") ~ "AL",
#       str_detect(df[[country_variable]], "AM") ~ "AM",
#       str_detect(df[[country_variable]], "AR|AR-Argentina") ~ "AR",
#       str_detect(df[[country_variable]], "AUS|AU|AUS-Australia|Australia") ~ "AU",
#       str_detect(df[[country_variable]], "AT|AT-Austria|A-Austria|^1$") ~ "AT",
#       str_detect(df[[country_variable]], "AZ") ~ "AZ",
#       str_detect(df[[country_variable]], "BA") ~ "BA",
#       str_detect(df[[country_variable]], "^BE$|BE-Belgium|^2$") ~ "BE",
#       str_detect(df[[country_variable]], "BG|BG-Bulgaria|^3$") ~ "BG",
#       str_detect(df[[country_variable]], "BY") ~ "BY",
#       str_detect(df[[country_variable]], "CA|CA-Canada|CDN-Canada|Canada - CDN") ~ "CA",
#       str_detect(df[[country_variable]], "CH|CH-Switzerland") ~ "CH",
#       str_detect(df[[country_variable]], "CL|CL-Chile|RCH-Chile") ~ "CL",
#       str_detect(df[[country_variable]], "CN|CN-China") ~ "CN",
#       str_detect(df[[country_variable]], "^CY_TCC$|^CY$|^4$") ~ "CY",
#       str_detect(df[[country_variable]], "CZ|CZ-Czech Republic|CR-Czech Republic|Czech Republic - CR|^5$") ~ "CZ",
#       str_detect(df[[country_variable]], "DE|DE-Germany|^6$|D-E|Germany-East|DE-E|D-W|Germany-West|DE-W") ~ "DE",
#       str_detect(df[[country_variable]], "DK|DK-Denmark|^7$") ~ "DK",
#       str_detect(df[[country_variable]], "EE|^8$") ~ "EE",
#       str_detect(df[[country_variable]], "ES|ES-Spain|E-Spain|Spain - E|10") ~ "ES",
#       str_detect(df[[country_variable]], "FI|FI-Finland|SF-Finland|11") ~ "FI",
#       str_detect(df[[country_variable]], "FR|FR-France|12") ~ "FR",
#       str_detect(df[[country_variable]], "^GB$|GB-Great Britain|GB-GBN|Great Britain|28") ~ "GB",
#       str_detect(df[[country_variable]], "GE") ~ "GE",
#       str_detect(df[[country_variable]], "GR|GR-Greece|Greece - GR|^9$") ~ "GR",
#       str_detect(df[[country_variable]], "HR|HR-Croatia|13") ~ "HR",
#       str_detect(df[[country_variable]], "HU|HU-Hungary|H-Hungary|Hungary - H|14") ~ "HU",
#       str_detect(df[[country_variable]], "IE|IRL-Ireland|Ireland - IRL|15") ~ "IE",
#       str_detect(df[[country_variable]], "IL|IL-Israel") ~ "IL",
#       str_detect(df[[country_variable]], "IN|IN-India") ~ "IN",
#       str_detect(df[[country_variable]], "IS|IS-Iceland|30") ~ "IS",
#       str_detect(df[[country_variable]], "IT|IT-Italy|I-Italy|Italy - I|16") ~ "IT",
#       str_detect(df[[country_variable]], "JP|JP-Japan|J-Japan|Japan - J") ~ "JP",
#       str_detect(df[[country_variable]], "KR|KR-Korea") ~ "KR",
#       str_detect(df[[country_variable]], "LU|LU-Luxembourg|Luxembourg - LU|18") ~ "LU",
#       str_detect(df[[country_variable]], "LV|LV-Latvia|19") ~ "LV",
#       str_detect(df[[country_variable]], "LT|LT-Lithuania|17") ~ "LT",
#       str_detect(df[[country_variable]], "ME|32") ~ "ME",
#       str_detect(df[[country_variable]], "MK|FYR of Macedonia|33") ~ "MK",
#       str_detect(df[[country_variable]], "MD") ~ "MD",
#       str_detect(df[[country_variable]], "MX|MX-Mexico|MEX-Mexico") ~ "MX",
#       str_detect(df[[country_variable]], "MT|20") ~ "MT",
#       str_detect(df[[country_variable]], "NL|NL-Netherlands|21") ~ "NL",
#       str_detect(df[[country_variable]], "NO|NO-Norway|N-Norway|Norway - N|34") ~ "NO",
#       str_detect(df[[country_variable]], "NIRL|NIRL-Northern Ireland|GB-NIR") ~ "GB_IRL",
#       str_detect(df[[country_variable]], "NZ|NZ-New Zealand") ~ "NZ",
#       str_detect(df[[country_variable]], "PH|PH-Philippines|RP-Philippines|Philippines - RP") ~ "PH",
#       str_detect(df[[country_variable]], "PL|PL-Poland|Poland - PL|22") ~ "PL",
#       str_detect(df[[country_variable]], "PT|PT-Portugal|P-Portugal|23") ~ "PT",
#       str_detect(df[[country_variable]], "RO|24") ~ "RO",
#       str_detect(df[[country_variable]], "RS|35") ~ "RS",
#       str_detect(df[[country_variable]], "RU|RU-Russia|RUS-Russia") ~ "RU",
#       str_detect(df[[country_variable]], "SE|SE-Sweden|S-Sweden|25") ~ "SE",
#       str_detect(df[[country_variable]], "SI|SI-Slovenia|SLO-Slovenia|Slovenia - SLO|26") ~ "SI",
#       str_detect(df[[country_variable]], "SK|SK-Slovakia|27") ~ "SK",
#       str_detect(df[[country_variable]], "TH|TH-Thailand") ~ "TH",
#       str_detect(df[[country_variable]], "TR|TR-Turkey|36") ~ "TR",
#       str_detect(df[[country_variable]], "TW") ~ "TW",
#       str_detect(df[[country_variable]], "UA") ~ "UA",
#       str_detect(df[[country_variable]], "US|US-United States|USA-United States") ~ "US",
#       str_detect(df[[country_variable]], "XK|31") ~ "XK",
#       str_detect(df[[country_variable]], "ZA|ZA-South Africa") ~ "ZA",
#       str_detect(df[[country_variable]], "^BE-FLA$") ~ "BE_FLA",
#       TRUE ~ df[[country_variable]]  
#     ))
# }

func_country_x <- function(df, country_variable) {
  df %>%
    mutate(country_source_recoded = case_when(
      str_detect(df[[country_variable]], "AL|29") ~ "AL",
      str_detect(df[[country_variable]], "AM") ~ "AM",
      str_detect(df[[country_variable]], "AR|AR-Argentina") ~ "AR",
      str_detect(df[[country_variable]], "AUS|AU|AUS-Australia|Australia") ~ "AU",
      str_detect(df[[country_variable]], "AT|AT-Austria|A-Austria|^1$") ~ "AT",
      str_detect(df[[country_variable]], "AZ") ~ "AZ",
      str_detect(df[[country_variable]], "BA") ~ "BA",
      str_detect(df[[country_variable]], "^BE$|BE-Belgium|^2$") ~ "BE",
      str_detect(df[[country_variable]], "BG|BG-Bulgaria|^3$") ~ "BG",
      str_detect(df[[country_variable]], "BY") ~ "BY",
      str_detect(df[[country_variable]], "CA|CA-Canada|CDN-Canada|Canada - CDN") ~ "CA",
      str_detect(df[[country_variable]], "CH|CH-Switzerland") ~ "CH",
      str_detect(df[[country_variable]], "CL|CL-Chile|RCH-Chile") ~ "CL",
      str_detect(df[[country_variable]], "CN|CN-China") ~ "CN",
      str_detect(df[[country_variable]], "^CY_TCC$|^CY$|^4$") ~ "CY",
      str_detect(df[[country_variable]], "CZ|CZ-Czech Republic|CR-Czech Republic|Czech Republic - CR|^5$") ~ "CZ",
      str_detect(df[[country_variable]], "DE|DE-Germany|^6$|D-E|Germany-East|DE-E|D-W|Germany-West|DE-W") ~ "DE",
      str_detect(df[[country_variable]], "DK|DK-Denmark|^7$") ~ "DK",
      str_detect(df[[country_variable]], "EE|^8$") ~ "EE",
      str_detect(df[[country_variable]], "ES|ES-Spain|E-Spain|Spain - E|10") ~ "ES",
      str_detect(df[[country_variable]], "FI|FI-Finland|SF-Finland|11") ~ "FI",
      str_detect(df[[country_variable]], "FR|FR-France|12") ~ "FR",
      str_detect(df[[country_variable]], "^GB$|GB-Great Britain|GB-GBN|Great Britain|28") ~ "GB",
      str_detect(df[[country_variable]], "GE") ~ "GE",
      str_detect(df[[country_variable]], "GR|GR-Greece|Greece - GR|^9$") ~ "GR",
      str_detect(df[[country_variable]], "HR|HR-Croatia|13") ~ "HR",
      str_detect(df[[country_variable]], "HU|HU-Hungary|H-Hungary|Hungary - H|14") ~ "HU",
      str_detect(df[[country_variable]], "IE|IRL-Ireland|Ireland - IRL|15") ~ "IE",
      str_detect(df[[country_variable]], "IL|IL-Israel") ~ "IL",
      str_detect(df[[country_variable]], "IN|IN-India") ~ "IN",
      str_detect(df[[country_variable]], "IS|IS-Iceland|30") ~ "IS",
      str_detect(df[[country_variable]], "IT|IT-Italy|I-Italy|Italy - I|16") ~ "IT",
      str_detect(df[[country_variable]], "JP|JP-Japan|J-Japan|Japan - J") ~ "JP",
      str_detect(df[[country_variable]], "KR|KR-Korea") ~ "KR",
      str_detect(df[[country_variable]], "LU|LU-Luxembourg|Luxembourg - LU|18") ~ "LU",
      str_detect(df[[country_variable]], "LV|LV-Latvia|19") ~ "LV",
      str_detect(df[[country_variable]], "LT|LT-Lithuania|17") ~ "LT",
      str_detect(df[[country_variable]], "ME|32") ~ "ME",
      str_detect(df[[country_variable]], "MK|FYR of Macedonia|33") ~ "MK",
      str_detect(df[[country_variable]], "MD") ~ "MD",
      str_detect(df[[country_variable]], "MX|MX-Mexico|MEX-Mexico") ~ "MX",
      str_detect(df[[country_variable]], "MT|20") ~ "MT",
      str_detect(df[[country_variable]], "NL|NL-Netherlands|21") ~ "NL",
      str_detect(df[[country_variable]], "NO|NO-Norway|N-Norway|Norway - N|34") ~ "NO",
      str_detect(df[[country_variable]], "NIRL|NIRL-Northern Ireland|GB-NIR") ~ "GB_IRL",
      str_detect(df[[country_variable]], "NZ|NZ-New Zealand") ~ "NZ",
      str_detect(df[[country_variable]], "PH|PH-Philippines|RP-Philippines|Philippines - RP") ~ "PH",
      str_detect(df[[country_variable]], "PL|PL-Poland|Poland - PL|22") ~ "PL",
      str_detect(df[[country_variable]], "PT|PT-Portugal|P-Portugal|23") ~ "PT",
      str_detect(df[[country_variable]], "RO|24") ~ "RO",
      str_detect(df[[country_variable]], "RS|35") ~ "RS",
      str_detect(df[[country_variable]], "RU|RU-Russia|RUS-Russia") ~ "RU",
      str_detect(df[[country_variable]], "SE|SE-Sweden|S-Sweden|25") ~ "SE",
      str_detect(df[[country_variable]], "SI|SI-Slovenia|SLO-Slovenia|Slovenia - SLO|26") ~ "SI",
      str_detect(df[[country_variable]], "SK|SK-Slovakia|27") ~ "SK",
      str_detect(df[[country_variable]], "TH|TH-Thailand") ~ "TH",
      str_detect(df[[country_variable]], "TR|TR-Turkey|36") ~ "TR",
      str_detect(df[[country_variable]], "TW") ~ "TW",
      str_detect(df[[country_variable]], "UA") ~ "UA",
      str_detect(df[[country_variable]], "US|US-United States|USA-United States") ~ "US",
      str_detect(df[[country_variable]], "XK|31") ~ "XK",
      str_detect(df[[country_variable]], "ZA|ZA-South Africa") ~ "ZA",
      str_detect(df[[country_variable]], "^BE-FLA$") ~ "BE_FLA",
      TRUE ~ df[[country_variable]]
    ))
}

func_country_sample <- function(df, country_variable) {
  df %>%
    mutate(country_source_recoded = case_when(
      str_detect(df[[country_variable]], "D-W|Germany-West|DE-W|DE_W") ~ "DE_W",
      str_detect(df[[country_variable]], "AL|29") ~ "AL",
      str_detect(df[[country_variable]], "AM") ~ "AM",
      str_detect(df[[country_variable]], "AR|AR-Argentina") ~ "AR",
      str_detect(df[[country_variable]], "AUS|AU|AUS-Australia|Australia") ~ "AU",
      str_detect(df[[country_variable]], "AT|AT-Austria|A-Austria|^1$") ~ "AT",
      str_detect(df[[country_variable]], "AZ") ~ "AZ",
      str_detect(df[[country_variable]], "BA") ~ "BA",
      str_detect(df[[country_variable]], "^BE$|BE-Belgium|^2$") ~ "BE",
      str_detect(df[[country_variable]], "BG|BG-Bulgaria|^3$") ~ "BG",
      str_detect(df[[country_variable]], "BY") ~ "BY",
      str_detect(df[[country_variable]], "CA|CA-Canada|CDN-Canada|Canada - CDN") ~ "CA",
      str_detect(df[[country_variable]], "CH|CH-Switzerland") ~ "CH",
      str_detect(df[[country_variable]], "CL|CL-Chile|RCH-Chile") ~ "CL",
      str_detect(df[[country_variable]], "CN|CN-China") ~ "CN",
      str_detect(df[[country_variable]], "^CY$|^4$") ~ "CY",
      str_detect(df[[country_variable]], "^CY-TCC$|CY-TCC") ~ "CY_TCC",
      str_detect(df[[country_variable]], "CZ|CZ-Czech Republic|CR-Czech Republic|Czech Republic - CR|^5$") ~ "CZ",
      str_detect(df[[country_variable]], "D-E|Germany-East|DE-E|DE_E") ~ "DE_E",
      str_detect(df[[country_variable]], "DE|DE-Germany|^6$") ~ "DE",
      str_detect(df[[country_variable]], "DK|DK-Denmark|^7$") ~ "DK",
      str_detect(df[[country_variable]], "EE|^8$") ~ "EE",
      str_detect(df[[country_variable]], "ES|ES-Spain|E-Spain|Spain - E|10") ~ "ES",
      str_detect(df[[country_variable]], "FI|FI-Finland|SF-Finland|11") ~ "FI",
      str_detect(df[[country_variable]], "FR|FR-France|12") ~ "FR",
      str_detect(df[[country_variable]], "^GB$|GB-Great Britain|GB-GBN|Great Britain") ~ "GB_GBN",
      str_detect(df[[country_variable]], "GE") ~ "GE",
      str_detect(df[[country_variable]], "GR|GR-Greece|Greece - GR|^9$") ~ "GR",
      str_detect(df[[country_variable]], "HR|HR-Croatia|13") ~ "HR",
      str_detect(df[[country_variable]], "HU|HU-Hungary|H-Hungary|Hungary - H|14") ~ "HU",
      str_detect(df[[country_variable]], "IE|IRL-Ireland|Ireland - IRL|15") ~ "IE",
      str_detect(df[[country_variable]], "IL|IL-Israel") ~ "IL",
      str_detect(df[[country_variable]], "IN|IN-India") ~ "IN",
      str_detect(df[[country_variable]], "IS|IS-Iceland|30") ~ "IS",
      str_detect(df[[country_variable]], "IT|IT-Italy|I-Italy|Italy - I|16") ~ "IT",
      str_detect(df[[country_variable]], "JP|JP-Japan|J-Japan|Japan - J") ~ "JP",
      str_detect(df[[country_variable]], "KR|KR-Korea") ~ "KR",
      str_detect(df[[country_variable]], "LU|LU-Luxembourg|Luxembourg - LU|18") ~ "LU",
      str_detect(df[[country_variable]], "LV|LV-Latvia|19") ~ "LV",
      str_detect(df[[country_variable]], "LT|LT-Lithuania|17") ~ "LT",
      str_detect(df[[country_variable]], "ME|32") ~ "ME",
      str_detect(df[[country_variable]], "MK|FYR of Macedonia|33") ~ "MK",
      str_detect(df[[country_variable]], "MD") ~ "MD",
      str_detect(df[[country_variable]], "MX|MX-Mexico|MEX-Mexico") ~ "MX", 
      str_detect(df[[country_variable]], "MT|20") ~ "MT",
      str_detect(df[[country_variable]], "NL|NL-Netherlands|21") ~ "NL",
      str_detect(df[[country_variable]], "NO|NO-Norway|N-Norway|Norway - N|34") ~ "NO",
      str_detect(df[[country_variable]], "NIRL|NIRL-Northern Ireland|GB-NIR") ~ "GB_NIR",
      str_detect(df[[country_variable]], "NZ|NZ-New Zealand") ~ "NZ",
      str_detect(df[[country_variable]], "PH|PH-Philippines|RP-Philippines|Philippines - RP") ~ "PH",
      str_detect(df[[country_variable]], "PL|PL-Poland|Poland - PL|22") ~ "PL",
      str_detect(df[[country_variable]], "PT|PT-Portugal|P-Portugal|23") ~ "PT",
      str_detect(df[[country_variable]], "RO|24") ~ "RO",
      str_detect(df[[country_variable]], "RS|35") ~ "RS",
      str_detect(df[[country_variable]], "RU|RU-Russia|RUS-Russia") ~ "RU",
      str_detect(df[[country_variable]], "SE|SE-Sweden|S-Sweden|25") ~ "SE",
      str_detect(df[[country_variable]], "SI|SI-Slovenia|SLO-Slovenia|Slovenia - SLO|26") ~ "SI",
      str_detect(df[[country_variable]], "SK|SK-Slovakia|27") ~ "SK",
      str_detect(df[[country_variable]], "TH|TH-Thailand") ~ "TH",
      str_detect(df[[country_variable]], "TR|TR-Turkey|36") ~ "TR",
      str_detect(df[[country_variable]], "TW") ~ "TW",
      str_detect(df[[country_variable]], "UA") ~ "UA",
      str_detect(df[[country_variable]], "28") ~ "GB_UK",
      str_detect(df[[country_variable]], "US|US-United States|USA-United States") ~ "US",
      str_detect(df[[country_variable]], "XK|31") ~ "XK",
      str_detect(df[[country_variable]], "ZA|ZA-South Africa") ~ "ZA",
      str_detect(df[[country_variable]], "^BE-FLA$") ~ "BE_FLA",
      TRUE ~ df[[country_variable]]
    ))
}

# func_country <- function(df, country_variable) {
#   df %>%
#     mutate(country_source_rec = case_when(
#       # Dynamically apply all country patterns using the list
#       str_detect(df[[country_variable]], "AL|29") ~ "AL",
#       str_detect(df[[country_variable]], "AM") ~ "AM",
#       str_detect(df[[country_variable]], "AR|AR-Argentina") ~ "AR",
#       str_detect(df[[country_variable]], "AUS|AU|AUS-Australia|Australia") ~ "AU",
#       str_detect(df[[country_variable]], "AT|AT-Austria|A-Austria|^1$") ~ "AT",
#       str_detect(df[[country_variable]], "AZ") ~ "AZ",
#       str_detect(df[[country_variable]], "BA") ~ "BA",
#       str_detect(df[[country_variable]], "^BE$|BE-Belgium|^2$") ~ "BE",
#       str_detect(df[[country_variable]], "BG|BG-Bulgaria|^3$") ~ "BG",
#       str_detect(df[[country_variable]], "BY") ~ "BY",
#       str_detect(df[[country_variable]], "CA|CA-Canada|CDN-Canada|Canada - CDN") ~ "CA",
#       str_detect(df[[country_variable]], "CH|CH-Switzerland") ~ "CH",
#       str_detect(df[[country_variable]], "CL|CL-Chile|RCH-Chile") ~ "CL",
#       str_detect(df[[country_variable]], "CN|CN-China") ~ "CN",
#       str_detect(df[[country_variable]], "^CY$|^4$") ~ "CY",
#       str_detect(df[[country_variable]], "^CY_TCC$") ~ "CY_TCC",
#       str_detect(df[[country_variable]], "CZ|CZ-Czech Republic|CR-Czech Republic|Czech Republic - CR|^5$") ~ "CZ",
#       str_detect(df[[country_variable]], "DE|DE-Germany|^6$|D-E|Germany-East|DE-E|D-W|Germany-West|DE-W") ~ "DE",
#       str_detect(df[[country_variable]], "D-E|Germany-East|DE-E") ~ "DE_E",
#       str_detect(df[[country_variable]], "D-W|Germany-West|DE-W") ~ "DE_W",
#       str_detect(df[[country_variable]], "DK|DK-Denmark|^7$") ~ "DK",
#       str_detect(df[[country_variable]], "EE|^8$") ~ "EE",
#       str_detect(df[[country_variable]], "ES|ES-Spain|E-Spain|Spain - E|10") ~ "ES",
#       str_detect(df[[country_variable]], "FI|FI-Finland|SF-Finland|11") ~ "FI",
#       str_detect(df[[country_variable]], "FR|FR-France|12") ~ "FR",
#       str_detect(df[[country_variable]], "^GB$|GB-Great Britain|GB-GBN|Great Britain") ~ "GB_GBN",
#       str_detect(df[[country_variable]], "GE") ~ "GE",
#       str_detect(df[[country_variable]], "GR|GR-Greece|Greece - GR|^9$") ~ "GR",
#       str_detect(df[[country_variable]], "HR|HR-Croatia|13") ~ "HR",
#       str_detect(df[[country_variable]], "HU|HU-Hungary|H-Hungary|Hungary - H|14") ~ "HU",
#       str_detect(df[[country_variable]], "IE|IRL-Ireland|Ireland - IRL|15") ~ "IE",
#       str_detect(df[[country_variable]], "IL|IL-Israel") ~ "IL",
#       str_detect(df[[country_variable]], "IN|IN-India") ~ "IN",
#       str_detect(df[[country_variable]], "IS|IS-Iceland|30") ~ "IS",
#       str_detect(df[[country_variable]], "IT|IT-Italy|I-Italy|Italy - I|16") ~ "IT",
#       str_detect(df[[country_variable]], "JP|JP-Japan|J-Japan|Japan - J") ~ "JP",
#       str_detect(df[[country_variable]], "KR|KR-Korea") ~ "KR",
#       str_detect(df[[country_variable]], "LU|LU-Luxembourg|Luxembourg - LU|18") ~ "LU",
#       str_detect(df[[country_variable]], "LV|LV-Latvia|19") ~ "LV",
#       str_detect(df[[country_variable]], "LT|LT-Lithuania|17") ~ "LT",
#       str_detect(df[[country_variable]], "ME|32") ~ "ME",
#       str_detect(df[[country_variable]], "MK|FYR of Macedonia|33") ~ "MK",
#       str_detect(df[[country_variable]], "MD") ~ "MD",
#       str_detect(df[[country_variable]], "MX|MX-Mexico|MEX-Mexico") ~ "MX",
#       str_detect(df[[country_variable]], "MT|20") ~ "MT",
#       str_detect(df[[country_variable]], "NL|NL-Netherlands|21") ~ "NL",
#       str_detect(df[[country_variable]], "NO|NO-Norway|N-Norway|Norway - N|34") ~ "NO",
#       str_detect(df[[country_variable]], "NIRL|NIRL-Northern Ireland|GB-NIR") ~ "GB_IRL",
#       str_detect(df[[country_variable]], "NZ|NZ-New Zealand") ~ "NZ",
#       str_detect(df[[country_variable]], "PH|PH-Philippines|RP-Philippines|Philippines - RP") ~ "PH",
#       str_detect(df[[country_variable]], "PL|PL-Poland|Poland - PL|22") ~ "PL",
#       str_detect(df[[country_variable]], "PT|PT-Portugal|P-Portugal|23") ~ "PT",
#       str_detect(df[[country_variable]], "RO|24") ~ "RO",
#       str_detect(df[[country_variable]], "RS|35") ~ "RS",
#       str_detect(df[[country_variable]], "RU|RU-Russia|RUS-Russia") ~ "RU",
#       str_detect(df[[country_variable]], "SE|SE-Sweden|S-Sweden|25") ~ "SE",
#       str_detect(df[[country_variable]], "SI|SI-Slovenia|SLO-Slovenia|Slovenia - SLO|26") ~ "SI",
#       str_detect(df[[country_variable]], "SK|SK-Slovakia|27") ~ "SK",
#       str_detect(df[[country_variable]], "TH|TH-Thailand") ~ "TH",
#       str_detect(df[[country_variable]], "TR|TR-Turkey|36") ~ "TR",
#       str_detect(df[[country_variable]], "TW") ~ "TW",
#       str_detect(df[[country_variable]], "UA") ~ "UA",
#       str_detect(df[[country_variable]], "28") ~ "GB_UK",
#       str_detect(df[[country_variable]], "US|US-United States|USA-United States") ~ "US",
#       str_detect(df[[country_variable]], "XK|31") ~ "XK",
#       str_detect(df[[country_variable]], "ZA|ZA-South Africa") ~ "ZA",
#       str_detect(df[[country_variable]], "^BE-FLA$") ~ "BE_FLA",
#       TRUE ~ df[[country_variable]]
#       ))
# }
      
#       TRUE ~ df[[country_variable]]  
#     ))
# }


#function to create labels 
country_new_labels <- function(df, country_variable) {
  df %>%
    mutate(country_new = labelled(
      .data[[country_variable]],
        labels = c(
        "AL" = "Albania",
        "AM" = "Armenia",
        "AR" = "Argentina",
        "AU" = "Australia",
        "AT" = "Austria",
        "AZ" = "Azerbaijan",
        "BA" = "Bosnia and Herzegovina",
        "BE" = "Belgium",
        "BG" = "Bulgaria",
        "BY" = "Belarus",
        "CA" = "Canada",
        "CH" = "Switzerland",
        "CL" = "Chile",
        "CN" = "China",
        "CY" = "Cyprus",
        "CZ" = "Czech Republic",
        "DE" = "Germany",
        "DK" = "Denmark",
        "EE" = "Estonia",
        "ES" = "Spain",
        "FI" = "Finland",
        "FR" = "France",
        "GB_GBN" = "Great Britian",
        "GB_UK" = "United Kingdom",
        "GE" = "Georgia",
        "GR" = "Greece",
        "HR" = "Croatia",
        "HU" = "Hungary",
        "IE" = "Ireland",
        "IL" = "Israel",
        "IN" = "India",
        "IS" = "Iceland",
        "IT" = "Italy",
        "JP" = "Japan",
        "KR" = "Korea",
        "LU" = "Luxembourg",
        "LV" = "Latvia",
        "LT" = "Lithuania",
        "ME" = "Montenegro",
        "MK" = "North Macedonia",
        "MD" = "Moldova",
        "MX" = "Mexico",
        "MT" = "Malta",
        "NL" = "Netherlands",
        "NO" = "Norway",
        "GB_IRL" = "Northern Ireland",
        "NZ" = "New Zealand",
        "PH" = "Philippines",
        "PL" = "Poland",
        "PT" = "Portugal",
        "RO" = "Romania",
        "RS" = "Serbia",
        "RU" = "Russia",
        "SE" = "Sweden",
        "SI" = "Slovenia",
        "SK" = "Slovakia",
        "TH" = "Thailand",
        "TR" = "Turkey",
        "TW" = "Taiwan",
        "UA" = "Ukraine",
        "US" = "United States",
        "ZA" = "South Africa",
        "XK" = "Kosovo"
      )
    ))
}

#function to create labels of country_source_recoded
country_source_recoded_labels <- function(df, country_variable) {
  df %>%
    mutate(country_source_recoded = labelled(
      .data[[country_variable]],
      labels = c(
        "AL" = "Albania",
        "AM" = "Armenia",
        "AR" = "Argentina",
        "AU" = "Australia",
        "AT" = "Austria",
        "AZ" = "Azerbaijan",
        "BA" = "Bosnia and Herzegovina",
        "BE" = "Belgium",
        "BG" = "Bulgaria",
        "BY" = "Belarus",
        "CA" = "Canada",
        "CH" = "Switzerland",
        "CL" = "Chile",
        "CN" = "China",
        "CY" = "Cyprus",
        "CY_TCC" = "Northern Cyprus",
        "CZ" = "Czech Republic",
        "DE" = "Germany",
        "DE_E" = "East Germany",
        "DE_W" = "West Germany",
        "DK" = "Denmark",
        "EE" = "Estonia",
        "ES" = "Spain",
        "FI" = "Finland",
        "FR" = "France",
        "GB_GBN" = "Great Britian",
        "GB_UK" = "United Kingdom",
        "GE" = "Georgia",
        "GR" = "Greece",
        "HR" = "Croatia",
        "HU" = "Hungary",
        "IE" = "Ireland",
        "IL" = "Israel",
        "IN" = "India",
        "IS" = "Iceland",
        "IT" = "Italy",
        "JP" = "Japan",
        "KR" = "Korea",
        "LU" = "Luxembourg",
        "LV" = "Latvia",
        "LT" = "Lithuania",
        "ME" = "Montenegro",
        "MK" = "North Macedonia",
        "MD" = "Moldova",
        "MX" = "Mexico",
        "MT" = "Malta",
        "NL" = "Netherlands",
        "NO" = "Norway",
        "GB_NIR" = "Northern Ireland",
        "NZ" = "New Zealand",
        "PH" = "Philippines",
        "PL" = "Poland",
        "PT" = "Portugal",
        "RO" = "Romania",
        "RS" = "Serbia",
        "RU" = "Russia",
        "SE" = "Sweden",
        "SI" = "Slovenia",
        "SK" = "Slovakia",
        "TH" = "Thailand",
        "TR" = "Turkey",
        "TW" = "Taiwan",
        "UA" = "Ukraine",
        "US" = "United States",
        "ZA" = "South Africa",
        "XK" = "Kosovo"
      )
    ))
}

labels_check <- c(
  "AL" = "Albania",
  "AM" = "Armenia",
  "AR" = "Argentina",
  "AU" = "Australia",
  "AT" = "Austria",
  "AZ" = "Azerbaijan",
  "BA" = "Bosnia and Herzegovina",
  "BE" = "Belgium",
  "BE_FLA" = "Belgium FLanders",
  "BG" = "Bulgaria",
  "BY" = "Belarus",
  "CA" = "Canada",
  "CH" = "Switzerland",
  "CL" = "Chile",
  "CN" = "China",
  "CY" = "Cyprus",
  "CZ" = "Czech Republic",
  "DE_E" = "East Germany after unification",
  "DE_W" = "West Germany after unification",
  "DE" = "Germany",
  "DK" = "Denmark",
  "EE" = "Estonia",
  "ES" = "Spain",
  "FI" = "Finland",
  "FR" = "France",
  "GB" = "Great Britain",
  "GE" = "Georgia",
  "GR" = "Greece",
  "HR" = "Croatia",
  "HU" = "Hungary",
  "IE" = "Ireland",
  "IL" = "Israel",
  "IN" = "India",
  "IS" = "Iceland",
  "IT" = "Italy",
  "JP" = "Japan",
  "KR" = "Korea",
  "LU" = "Luxembourg",
  "LV" = "Latvia",
  "LT" = "Lithuania",
  "ME" = "Montenegro",
  "MK" = "North Macedonia",
  "MD" = "Moldova",
  "MX" = "Mexico",
  "MT" = "Malta",
  "NL" = "Netherlands",
  "NO" = "Norway",
  "GB_IRL" = "Northern Ireland",
  "NZ" = "New Zealand",
  "PH" = "Philippines",
  "PL" = "Poland",
  "PT" = "Portugal",
  "RO" = "Romania",
  "RS" = "Serbia",
  "RU" = "Russia",
  "SE" = "Sweden",
  "SI" = "Slovenia",
  "SK" = "Slovakia",
  "TH" = "Thailand",
  "TR" = "Turkey",
  "TW" = "Taiwan",
  "UA" = "Ukraine",
  "US" = "United States",
  "ZA" = "South Africa",
  "XK" = "Kosovo"
)




# Function to create numeric country variable 
num_recoded_country <- function(df, country_variable) {
  df %>%
    mutate(num_country_new = case_when(
      # Dynamically apply all country patterns using the list
      str_detect(df[[country_variable]], "Albania") ~ "008",
      str_detect(df[[country_variable]], "Argentina") ~ "032",
      str_detect(df[[country_variable]], "Armenia") ~ "051",
      str_detect(df[[country_variable]], "Australia") ~ "036",
      str_detect(df[[country_variable]], "Austria") ~ "040",
      str_detect(df[[country_variable]], "Azerbaijan") ~ "031",
      str_detect(df[[country_variable]], "Belgium") ~ "056",
      str_detect(df[[country_variable]], "Belarus") ~ "112",
      str_detect(df[[country_variable]], "Bosnia and Herzegovina") ~ "070",
      str_detect(df[[country_variable]], "Bulgaria") ~ "100",
      str_detect(df[[country_variable]], "Canada") ~ "124",
      str_detect(df[[country_variable]], "Chile") ~ "152",
      str_detect(df[[country_variable]], "China") ~ "156",
      str_detect(df[[country_variable]], "Croatia") ~ "191",
      str_detect(df[[country_variable]], "Cyprus") ~ "196",
      str_detect(df[[country_variable]], "Czechia") ~ "203",
      str_detect(df[[country_variable]], "Denmark") ~ "208",
      str_detect(df[[country_variable]], "Estonia") ~ "233",
      str_detect(df[[country_variable]], "Finland") ~ "246",
      str_detect(df[[country_variable]], "France") ~ "250",
      str_detect(df[[country_variable]], "Georgia") ~ "268",
      str_detect(df[[country_variable]], "Germany") ~ "276",
      str_detect(df[[country_variable]], "East Germany") ~ "27602",
      str_detect(df[[country_variable]], "West Germany") ~ "27601",
      str_detect(df[[country_variable]], "Greece") ~ "300",
      str_detect(df[[country_variable]], "Hungary") ~ "348",
      str_detect(df[[country_variable]], "Iceland") ~ "352",
      str_detect(df[[country_variable]], "India") ~ "356",
      str_detect(df[[country_variable]], "Ireland") ~ "372",
      str_detect(df[[country_variable]], "Israel") ~ "376",
      str_detect(df[[country_variable]], "Italy") ~ "380",
      str_detect(df[[country_variable]], "Japan") ~ "392",
      str_detect(df[[country_variable]], "Korea") ~ "410",
      str_detect(df[[country_variable]], "Latvia") ~ "428",
      str_detect(df[[country_variable]], "Lithuania") ~ "438",
      str_detect(df[[country_variable]], "Luxembourg") ~ "442",
      str_detect(df[[country_variable]], "Malta") ~ "470",
      str_detect(df[[country_variable]], "Mexico") ~ "484",
      str_detect(df[[country_variable]], "Moldova") ~ "498",
      str_detect(df[[country_variable]], "Montenegro") ~ "499",
      str_detect(df[[country_variable]], "Netherlands") ~ "528",
      str_detect(df[[country_variable]], "New Zealand") ~ "554",
      str_detect(df[[country_variable]], "Norway") ~ "578",
      str_detect(df[[country_variable]], "North Macedonia") ~ "807",
      str_detect(df[[country_variable]], "Philippines") ~ "608",
      str_detect(df[[country_variable]], "Poland") ~ "616",
      str_detect(df[[country_variable]], "Portugal") ~ "620",
      str_detect(df[[country_variable]], "Romania") ~ "642",
      str_detect(df[[country_variable]], "Russia") ~ "643",
      str_detect(df[[country_variable]], "Serbia") ~ "688",
      str_detect(df[[country_variable]], "Slovakia") ~ "703",
      str_detect(df[[country_variable]], "Slovenia") ~ "705",
      str_detect(df[[country_variable]], "South Africa") ~ "710",
      str_detect(df[[country_variable]], "Spain") ~ "724",
      str_detect(df[[country_variable]], "Sweden") ~ "752",
      str_detect(df[[country_variable]], "Switzerland") ~ "756",
      str_detect(df[[country_variable]], "Taiwan") ~ "158",
      str_detect(df[[country_variable]], "Thailand") ~ "764",
      str_detect(df[[country_variable]], "Turkey") ~ "792",
      str_detect(df[[country_variable]], "Ukraine") ~ "804",
      str_detect(df[[country_variable]], "United Kingdom") ~ "826",
      str_detect(df[[country_variable]], "Northern Ireland") ~ "82602",
      str_detect(df[[country_variable]], "United States") ~ "840",
      TRUE ~ df[[country_variable]]  # Default if no match
    ))%>%
    mutate(num_country_new = as.numeric(as.character(num_country_new))) # Ensure num_country_new is numeric here
}


# Object to assign labels 
lab <- c(
  Albania = 8,
  Argentina = 32,
  Armenia = 51,
  Australia = 36,
  Austria = 40,
  Azerbaijan = 31,
  `Bosnia and Herzegovina` = 70,
  Belgium = 56,
  Bulgaria = 100,
  Belarus = 112,
  Canada = 124,
  Switzerland = 756,
  Chile = 152,
  China = 156,
  Cyprus = 196,
  `Czech Republic` = 203,
  `East Germany` = 27602,
  `West Germany` = 27601,
  Germany = 276,
  Denmark = 208,
  Estonia = 233,
  Spain = 724,
  Finland = 246,
  France = 250,
  `United Kingdom` = 826,
  Georgia = 268,
  Greece = 300,
  Croatia = 191,
  Hungary = 348,
  Ireland = 372,
  Israel = 376,
  India = 356,
  Iceland = 352,
  Italy = 380,
  Japan = 392,
  Korea = 410,
  Luxembourg = 442,
  Latvia = 428,
  Lithuania = 440,
  Montenegro = 499,
  `North Macedonia` = 807,
  Moldova = 498,
  Mexico = 484,
  Malta = 470,
  Netherlands = 528,
  Norway = 578,
  `Northern Ireland` = 82602,
  `New Zealand` = 554,
  Philippines = 608,
  Poland = 616,
  Portugal = 620,
  Romania = 642,
  Serbia = 688,
  Russia = 643,
  Sweden = 752,
  Slovenia = 705,
  Slovakia = 703,
  Thailand = 764,
  Turkey = 792,
  Taiwan = 158,
  Ukraine = 804,
  `United States` = 840,
  `South Africa` = 710
)



#code snippet to use for a country sample function (country selection comes form green pillar)
# #function to create labels 
# country_new_labels <- function(df, country_variable) {
#   df %>%
#     mutate(country_new = labelled(
#       .data[[country_variable]],
#       labels = c(
#         "AL" = "Albania",
#         "AM" = "Armenia",
#         "AR" = "Argentina",
#         "AU" = "Australia",
#         "AT" = "Austria",
#         "AZ" = "Azerbaijan",
#         "BA" = "Bosnia and Herzegovina",
#         "BE" = "Belgium",
#         "BE_FLA" = "Belgium Flanders",
#         "BG" = "Bulgaria",
#         "BY" = "Belarus",
#         "CA" = "Canada",
#         "CH" = "Switzerland",
#         "CL" = "Chile",
#         "CN" = "China",
#         "CY_TCC" = "Nothern Cyprus",
#         "CY" = "Cyprus",
#         "CZ" = "Czech Republic",
#         "DE_E" = "East Germany after unification",
#         "DE_W" = "West Germany after unification",
#         "DE" = "Germany",
#         "DK" = "Denmark",
#         "EE" = "Estonia",
#         "ES" = "Spain",
#         "FI" = "Finland",
#         "FR" = "France",
#         "GB" = "Great Britian",
#         "GE" = "Georgia",
#         "GR" = "Greece",
#         "HR" = "Croatia",
#         "HU" = "Hungary",
#         "IE" = "Ireland",
#         "IL" = "Israel",
#         "IN" = "India",
#         "IS" = "Iceland",
#         "IT" = "Italy",
#         "JP" = "Japan",
#         "KR" = "Korea",
#         "LU" = "Luxembourg",
#         "LV" = "Latvia",
#         "LT" = "Lithuania",
#         "ME" = "Montenegro",
#         "MK" = "North Macedonia",
#         "MD" = "Moldova",
#         "MX" = "Mexico",
#         "MT" = "Malta",
#         "NL" = "Netherlands",
#         "NO" = "Norway",
#         "GB_IRL" = "Northern Ireland",
#         "NZ" = "New Zealand",
#         "PH" = "Philippines",
#         "PL" = "Poland",
#         "PT" = "Portugal",
#         "RO" = "Romania",
#         "RS" = "Serbia",
#         "RU" = "Russia",
#         "SE" = "Sweden",
#         "SI" = "Slovenia",
#         "SK" = "Slovakia",
#         "TH" = "Thailand",
#         "TR" = "Turkey",
#         "TW" = "Taiwan",
#         "UA" = "Ukraine",
#         "US" = "United States",
#         "ZA" = "South Africa",
#         "XK" = "Kosovo"
#       )
#     ))
# }



# country_new_labels <- function(df, country_variable) {
#   df %>%
#     mutate(country_new = labelled(
#       country_new,
#       labels = c(
#         "AL" = "Albania",
#         "AM" = "Armenia",
#         "AR" = "Argentina",
#         "AU" = "Australia",
#         "AT" = "Austria",
#         "AZ" = "Azerbaijan",
#         "BA" = "Bosnia and Herzegovina",
#         "BE" = "Belgium",
#         "BE_FLA" = "Belgium FLanders",
#         "BG" = "Bulgaria",
#         "BY" = "Belarus",
#         "CA" = "Canada",
#         "CH" = "Switzerland",
#         "CL" = "Chile",
#         "CN" = "China",
#         "CY" = "Cyprus",
#         "CZ" = "Czech Republic",
#         "DE_E" = "East Germany after unification",
#         "DE_W" = "West Germany after unification",
#         "DE" = "Germany",
#         "DK" = "Denmark",
#         "EE" = "Estonia",
#         "ES" = "Spain",
#         "FI" = "Finland",
#         "FR" = "France",
#         "GB" = "Great Britian",
#         "GE" = "Georgia",
#         "GR" = "Greece",
#         "HR" = "Croatia",
#         "HU" = "Hungary",
#         "IE" = "Ireland",
#         "IL" = "Israel",
#         "IN" = "India",
#         "IS" = "Iceland",
#         "IT" = "Italy",
#         "JP" = "Japan",
#         "KR" = "Korea",
#         "LU" = "Luxembourg",
#         "LV" = "Latvia",
#         "LT" = "Lithuania",
#         "ME" = "Montenegro",
#         "MK" = "North Macedonia",
#         "MD" = "Moldova",
#         "MX" = "Mexico",
#         "MT" = "Malta",
#         "NL" = "Netherlands",
#         "NO" = "Norway",
#         "GB_IRL" = "Northern Ireland",
#         "NZ" = "New Zealand",
#         "PH" = "Philippines",
#         "PL" = "Poland",
#         "PT" = "Portugal",
#         "RO" = "Romania",
#         "RS" = "Serbia",
#         "RU" = "Russia",
#         "SE" = "Sweden",
#         "SI" = "Slovenia",
#         "SK" = "Slovakia",
#         "TH" = "Thailand",
#         "TR" = "Turkey",
#         "TW" = "Taiwan",
#         "UA" = "Ukraine",
#         "US" = "United States",
#         "ZA" = "South Africa",
#         "XK" = "Kosovo"
#         ),
#       default = "Unknown"
#     ))
# }

# code snippet of the country_sample code: problem: DE-W gets overwritten by DE
# Dynamically apply all country patterns using the list
#       str_detect(df[[country_variable]], "AL|29") ~ "AL",
#       str_detect(df[[country_variable]], "AM") ~ "AM",
#       str_detect(df[[country_variable]], "AR|AR-Argentina") ~ "AR",
#       str_detect(df[[country_variable]], "AUS|AU|AUS-Australia|Australia") ~ "AU",
#       str_detect(df[[country_variable]], "AT|AT-Austria|A-Austria|^1$") ~ "AT",
#       str_detect(df[[country_variable]], "AZ") ~ "AZ",
#       str_detect(df[[country_variable]], "BA") ~ "BA",
#       str_detect(df[[country_variable]], "^BE-FLA$") ~ "BE_FLA",
#       str_detect(df[[country_variable]], "^BE$|BE-Belgium|^2$") ~ "BE",
#       str_detect(df[[country_variable]], "BG|BG-Bulgaria|^3$") ~ "BG",
#       str_detect(df[[country_variable]], "BY") ~ "BY",
#       str_detect(df[[country_variable]], "CA|CA-Canada|CDN-Canada|Canada - CDN") ~ "CA",
#       str_detect(df[[country_variable]], "CH|CH-Switzerland") ~ "CH",
#       str_detect(df[[country_variable]], "CL|CL-Chile|RCH-Chile") ~ "CL",
#       str_detect(df[[country_variable]], "CN|CN-China") ~ "CN",
#       str_detect(df[[country_variable]], "^CY_TCC$") ~ "CY_TCC",
#       str_detect(df[[country_variable]], "^CY$|^4$") ~ "CY",
#       str_detect(df[[country_variable]], "CZ|CZ-Czech Republic|CR-Czech Republic|Czech Republic - CR|^5$") ~ "CZ",
#       str_detect(df[[country_variable]], "^D-W$|Germany-West|^DE-W$") ~ "DE_W",
#       str_detect(df[[country_variable]], "^D-E$|Germany-East|^DE-E$") ~ "DE_E",
#       str_detect(df[[country_variable]], "^DE$|DE-Germany|^6$") ~ "DE",
#       str_detect(df[[country_variable]], "DK|DK-Denmark|^7$") ~ "DK",
#       str_detect(df[[country_variable]], "EE|^8$") ~ "EE",
#       str_detect(df[[country_variable]], "ES|ES-Spain|E-Spain|Spain - E|10") ~ "ES",
#       str_detect(df[[country_variable]], "FI|FI-Finland|SF-Finland|11") ~ "FI",
#       str_detect(df[[country_variable]], "FR|FR-France|12") ~ "FR",
#       str_detect(df[[country_variable]], "GB-NIR") ~ "GB_NIR",
#       str_detect(df[[country_variable]], "^GB$|GB-Great Britain|GB-GBN|Great Britain|28") ~ "GB",
#       str_detect(df[[country_variable]], "GE") ~ "GE",
#       str_detect(df[[country_variable]], "GR|GR-Greece|Greece - GR|^9$") ~ "GR",
#       str_detect(df[[country_variable]], "HR|HR-Croatia|13") ~ "HR",
#       str_detect(df[[country_variable]], "HU|HU-Hungary|H-Hungary|Hungary - H|14") ~ "HU",
#       str_detect(df[[country_variable]], "IE|IRL-Ireland|Ireland - IRL|15") ~ "IE",
#       str_detect(df[[country_variable]], "IL|IL-Israel") ~ "IL",
#       str_detect(df[[country_variable]], "IN|IN-India") ~ "IN",
#       str_detect(df[[country_variable]], "IS|IS-Iceland|30") ~ "IS",
#       str_detect(df[[country_variable]], "IT|IT-Italy|I-Italy|Italy - I|16") ~ "IT",
#       str_detect(df[[country_variable]], "JP|JP-Japan|J-Japan|Japan - J") ~ "JP",
#       str_detect(df[[country_variable]], "KR|KR-Korea") ~ "KR",
#       str_detect(df[[country_variable]], "LU|LU-Luxembourg|Luxembourg - LU|18") ~ "LU",
#       str_detect(df[[country_variable]], "LV|LV-Latvia|19") ~ "LV",
#       str_detect(df[[country_variable]], "LT|LT-Lithuania|17") ~ "LT",
#       str_detect(df[[country_variable]], "ME|32") ~ "ME",
#       str_detect(df[[country_variable]], "MK|FYR of Macedonia|33") ~ "MK",
#       str_detect(df[[country_variable]], "MD") ~ "MD",
#       str_detect(df[[country_variable]], "MX|MX-Mexico|MEX-Mexico") ~ "MX",
#       str_detect(df[[country_variable]], "MT|20") ~ "MT",
#       str_detect(df[[country_variable]], "NL|NL-Netherlands|21") ~ "NL",
#       str_detect(df[[country_variable]], "NO|NO-Norway|N-Norway|Norway - N|34") ~ "NO",
#       str_detect(df[[country_variable]], "NIRL|NIRL-Northern Ireland") ~ "GB_IRL",
#       str_detect(df[[country_variable]], "NZ|NZ-New Zealand") ~ "NZ",
#       str_detect(df[[country_variable]], "PH|PH-Philippines|RP-Philippines|Philippines - RP") ~ "PH",
#       str_detect(df[[country_variable]], "PL|PL-Poland|Poland - PL|22") ~ "PL",
#       str_detect(df[[country_variable]], "PT|PT-Portugal|P-Portugal|23") ~ "PT",
#       str_detect(df[[country_variable]], "RO|24") ~ "RO",
#       str_detect(df[[country_variable]], "RS|35") ~ "RS",
#       str_detect(df[[country_variable]], "RU|RU-Russia|RUS-Russia") ~ "RU",
#       str_detect(df[[country_variable]], "SE|SE-Sweden|S-Sweden|25") ~ "SE",
#       str_detect(df[[country_variable]], "SI|SI-Slovenia|SLO-Slovenia|Slovenia - SLO|26") ~ "SI",
#       str_detect(df[[country_variable]], "SK|SK-Slovakia|27") ~ "SK",
#       str_detect(df[[country_variable]], "TH|TH-Thailand") ~ "TH",
#       str_detect(df[[country_variable]], "TR|TR-Turkey|36") ~ "TR",
#       str_detect(df[[country_variable]], "TW") ~ "TW",
#       str_detect(df[[country_variable]], "UA") ~ "UA",
#       str_detect(df[[country_variable]], "US|US-United States|USA-United States") ~ "US",
#       str_detect(df[[country_variable]], "XK|31") ~ "XK",
#       str_detect(df[[country_variable]], "ZA|ZA-South Africa") ~ "ZA",
#       TRUE ~ df[[country_variable]]  
# ),
# country_lng = case_when(
#   str_detect(df[[country_variable]], "AL|29") ~ "Albania",
#   str_detect(df[[country_variable]], "AM") ~ "Armenia",
#   str_detect(df[[country_variable]], "AR|AR-Argentina") ~ "Argentina",
#   str_detect(df[[country_variable]], "AUS|AU|AUS-Australia|Australia") ~ "Australia",
#   str_detect(df[[country_variable]], "AT|AT-Austria|A-Austria|^1$") ~ "Austria",
#   str_detect(df[[country_variable]], "AZ") ~ "Azerbaijan",
#   str_detect(df[[country_variable]], "BA") ~ "Bosnia and Herzegovina",
#   str_detect(df[[country_variable]], "^BE-FLA$") ~ "Belgium Flanders",
#   str_detect(df[[country_variable]], "^BE$|BE-Belgium|^2$") ~ "Belgium",
#   str_detect(df[[country_variable]], "BG|BG-Bulgaria|^3$") ~ "Bulgaria",
#   str_detect(df[[country_variable]], "BY") ~ "Belarus",
#   str_detect(df[[country_variable]], "CA|CA-Canada|CDN-Canada|Canada - CDN") ~ "Canada",
#   str_detect(df[[country_variable]], "CH|CH-Switzerland") ~ "Switzerland",
#   str_detect(df[[country_variable]], "CL|CL-Chile|RCH-Chile") ~ "Chile",
#   str_detect(df[[country_variable]], "CN|CN-China") ~ "China",
#   str_detect(df[[country_variable]], "^CY_TCC$") ~ "Nothern Cyprus",
#   str_detect(df[[country_variable]], "CY|^4$") ~ "Cyprus",  
#   str_detect(df[[country_variable]], "CZ|CZ-Czech Republic|CR-Czech Republic|Czech Republic - CR|^5$") ~ "Czech Republic",
#   str_detect(df[[country_variable]], "D-E|Germany-East") ~ "East Germany",
#   str_detect(df[[country_variable]], "D-W|Germany-West") ~ "West Germany",
#   str_detect(df[[country_variable]], "DE|DE-Germany|^6$") ~ "Germany",
#   str_detect(df[[country_variable]], "^DK$|DK-Denmark|^7$") ~ "Denmark",
#   str_detect(df[[country_variable]], "EE|^8$") ~ "Estonia",
#   str_detect(df[[country_variable]], "ES|ES-Spain|E-Spain|Spain - E|^10$") ~ "Spain",
#   str_detect(df[[country_variable]], "FI|FI-Finland|SF-Finland|11") ~ "Finland",
#   str_detect(df[[country_variable]], "FR|FR-France|12") ~ "France",
#   str_detect(df[[country_variable]], "GB|GB-Great Britain|GB-GBN|Great Britain|28") ~ "Great Britain",
#   str_detect(df[[country_variable]], "GE") ~ "Georgia",
#   str_detect(df[[country_variable]], "GR|GR-Greece|Greece - GR|^9$") ~ "Greece",
#   str_detect(df[[country_variable]], "HR|HR-Croatia|13") ~ "Croatia",
#   str_detect(df[[country_variable]], "HU|HU-Hungary|H-Hungary|Hungary - H|14") ~ "Hungary",
#   str_detect(df[[country_variable]], "IE|IRL-Ireland|Ireland - IRL|15") ~ "Ireland",
#   str_detect(df[[country_variable]], "IL|IL-Israel") ~ "Israel",
#   str_detect(df[[country_variable]], "IN|IN-India") ~ "India",
#   str_detect(df[[country_variable]], "IS|IS-Iceland|30") ~ "Iceland",
#   str_detect(df[[country_variable]], "IT|IT-Italy|I-Italy|Italy - I|16") ~ "Italy",
#   str_detect(df[[country_variable]], "JP|JP-Japan|J-Japan|Japan - J") ~ "Japan",
#   str_detect(df[[country_variable]], "KR|KR-Korea") ~ "Korea",
#   str_detect(df[[country_variable]], "LU|LU-Luxembourg|Luxembourg - LU|18") ~ "Luxembourg",
#   str_detect(df[[country_variable]], "LV|LV-Latvia|19") ~ "Latvia",
#   str_detect(df[[country_variable]], "LT|LT-Lithuania|17") ~ "Lithuania",
#   str_detect(df[[country_variable]], "^ME$|^32$") ~ "Montenegro",
#   str_detect(df[[country_variable]], "MK|FYR of Macedonia|33") ~ "North Macedonia",
#   str_detect(df[[country_variable]], "MD") ~ "Moldova",
#   str_detect(df[[country_variable]], "MX|MX-Mexico|MEX-Mexico") ~ "Mexico",
#   str_detect(df[[country_variable]], "MT|20") ~ "Malta",
#   str_detect(df[[country_variable]], "NL|NL-Netherlands|21") ~ "Netherlands",
#   str_detect(df[[country_variable]], "NO|NO-Norway|N-Norway|Norway - N|34") ~ "Norway",
#   str_detect(df[[country_variable]], "GB-NIR|NIRL|NIRL-Northern Ireland") ~ "Northern Ireland",
#   str_detect(df[[country_variable]], "NZ|NZ-New Zealand") ~ "New Zealand",
#   str_detect(df[[country_variable]], "PH|PH-Philippines|RP-Philippines|Philippines - RP") ~ "Philippines",
#   str_detect(df[[country_variable]], "PL|PL-Poland|Poland - PL|22") ~ "Poland",
#   str_detect(df[[country_variable]], "PT|PT-Portugal|P-Portugal|23") ~ "Portugal",
#   str_detect(df[[country_variable]], "RO|24") ~ "Romania",
#   str_detect(df[[country_variable]], "RS|35") ~ "Serbia",
#   str_detect(df[[country_variable]], "RU|RU-Russia|RUS-Russia") ~ "Russia",
#   str_detect(df[[country_variable]], "SE|SE-Sweden|S-Sweden|25") ~ "Sweden",
#   str_detect(df[[country_variable]], "SI|SI-Slovenia|SLO-Slovenia|Slovenia - SLO|26") ~ "Slovenia",
#   str_detect(df[[country_variable]], "SK|SK-Slovakia|27") ~ "Slovakia",
#   str_detect(df[[country_variable]], "TH|TH-Thailand") ~ "Thailand",
#   str_detect(df[[country_variable]], "TR|TR-Turkey|36") ~ "Turkey",
#   str_detect(df[[country_variable]], "TW") ~ "Taiwan",
#   str_detect(df[[country_variable]], "UA") ~ "Ukraine",
#   str_detect(df[[country_variable]], "US|US-United States|USA-United States") ~ "United States",
#   str_detect(df[[country_variable]], "ZA|ZA-South Africa") ~ "South Africa",
#   str_detect(df[[country_variable]], "XK|31") ~ "Kosovo",
#   TRUE ~ df[[country_variable]]  
#   )
#  )
# }


# Example of how to apply recoded_country, num_recoded_country and the labels

# # Load data
# ESS1 <- read_sav("I:/Team_IntUm/Infra4NextGen/Survey_Programmes/ESS/data/ESS1e06_7.sav")
# 
# # Apply recoded_country function 
# # (change "cntry" if country variable named differently in source data)
# ESS1_new_cntry <- recoded_country(ESS1, "cntry")
# 
# # Apply num_recoded_country function
# ESS1_new_cntry <- num_recoded_country(ESS1_new_cntry, "country_new")
# 
# # Set labels
# ESS1_new_cntry <- ESS1_new_cntry %>%
# labelled::set_value_labels(num_country_new = lab)
# labelled::val_labels(ESS1_new_cntry$num_country_new)



