Skip to content

Instantly share code, notes, and snippets.

@JosepER
Created March 8, 2023 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JosepER/33e27596322b3650eca773732de5c34c to your computer and use it in GitHub Desktop.
Save JosepER/33e27596322b3650eca773732de5c34c to your computer and use it in GitHub Desktop.
Example of functions to get OECD National Account data
#' Download the OECD detailed National Accounts
#'
#' @description
#' Uses the API to the OECD tables to download the National Accounts.
#'
#' Downloads all variables for all years for the selected countries.
#'
#' @details
#' The OECD API takes countries with ISO3. The function uses the METIS 'country'
#' table to convert ISO2 to ISO3.
#'
#' @param countries A string with the list of countries for which the
#' National Accounts should be downloaded. Country names should be in ISO2
#' or ISO3. If NULL (default), the National Accounts for all countries in
#' METIS datasets will be downloaded.
#' @param start_year An integer with the year from which the funciton should
#' start reading the National Accounts. By default it starts at 1967.
#'
#' @return A tibble with 5 columns: country, variable, sector, year, value.
download_oecd_detailed <- function(countries = NULL, start_year = 1967){
countries <- toupper(countries)
assertthat::assert_that(all(stringr::str_length(countries) %in% c(2, 3)),
msg = "Values in 'countries' need to be country codes in ISO2 or ISO3.")
if(any(stringr::str_length(countries) == 2)){
countries <- convert_iso2_to_iso3(countries)
}
variables <- c("NFB2G_B3GR", "NFB2G_B3GP", "NFB2GP", "NFB2GR",
"NFB3GR", "NFB3GP", "NFB3GR",
"NFD11P", "NFD12R", "NFD11R", "NFD1P", "NFD11P", "NFD12P", "NFD1R", "NFD12R",
"NFD4R", "NFD4P",
"NFD41P", "NFD41R", "NFD42R", "NFD42P", "NFD43P", "NFD43R", "NFD44R",
"NFD44P", "NFD45P", "NFD45R", "NFD421P", "NFD421R", "NFD441R", "NFD441P",
"NFD442R", "NFD422R", "NFD442P", "NFD422P", "NFD443P", "NFD443R",
"NFD51P", "NFD51R", "NFD5P", "NFD5R", "NFD59P", "NFD59R",
"NFD61P", "NFD61R", "NFD611P", "NFD611R", "NFD6P", "NFD612R", "NFD612P",
"NFD62P", "NFD62R", "NFD63P", "NFD63R", "NFD6R", "NFD631P", "NFD631R",
"NFD6111P", "NFD6111R", "NFD6121P", "NFD6121R", "NFD612HP", "NFD612HR",
"NFD611HP", "NFD611HR", "NFD632P", "NFD632R", "NFD61SCP", "NFD61SCR",
"NFB5GR", "NFB5GP",
"NFD71P", "NFD71R", "NFD72R", "NFD72P", "NFD74P", "NFD74R", "NFD7P",
"NFD7R", "NFD75R", "NFD75P", "NFD76P", "NFD76R", "NFD751R", "NFD751P",
"NFB6GR", "NFB6GP",
"NFB7GP",
"NFP31P", "NFP3P", "NFP32P",
"NFD8P", "NFD8R",
"NFK1MP", "NFK1P", "NFK1R",
"NFTINTR" # instead of "NFD41GR"
)
downloaded_df <- OECD::get_dataset("SNA_TABLE14A",
filter = list(countries,
variables,
c("S1", "S14", "S14_S15")),
start_time = start_year)
check_duplicates_downloaded_oecd_detailed(downloaded_df)
# subset columns
return(dplyr::select(downloaded_df, country = LOCATION, variable = TRANSACT,
sector = SECTOR, year = Time, value = ObsValue))
}
#' Check for duplicates in downloaded OECD National Accounts
#'
#' @description
#' Throws an error if there are duplicates in the downloaded OECD data.
#'
#' @param downloaded_df A data.frame type of object with the downloaded data.
#'
#' @return NULL if there are no duplicates. Throws an error otherwise.
check_duplicates_downloaded_oecd_detailed <- function(downloaded_df){
nrow_df <- nrow(dplyr::select(downloaded_df, LOCATION, TRANSACT, SECTOR, Time))
nrow_unique_df <- nrow(unique(dplyr::select(downloaded_df, LOCATION, TRANSACT, SECTOR, Time)))
assertthat::assert_that(nrow_df == nrow_unique_df,
msg = "The number of rows in the downloaded dataset is not the same as the number of unique combinations of 'LOCATION', 'TRANSACT', 'SECTOR' and 'Time'. There might be duplicated rows in the downloaded dataset.")
return(invisible(NULL))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment