Created
November 29, 2021 09:54
-
-
Save agnesevardanega/7b0a73d6d03ca4ff808ca863c12e81fa to your computer and use it in GitHub Desktop.
Recupera i campi ed eventualmente le etichette (in italiano e in inglese) dei flussi di dati sdmx dell'Istat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' cod_sdmx | |
#' | |
#' Recupera i campi e le etichette in italiano e in inglese dei flussi di dati | |
#' sdmx dell'Istat. | |
#' The function fetches dimensions (fields) and labels in Italian and English, | |
#' for Istat sdmx dataflows. | |
#' | |
#' @param rid resource ID | |
#' @param label recupera solo i campi (FALSE), o anche le etichette (TRUE); | |
#' if FALSE only dimensions (fields) will be fetched. | |
#' @return Il dataframe dei campi, o una lista con il dataframe dei campi | |
#' e quello delle etichette | |
#' @examples cod_sdmx(rid = "DCCV_TAXOCCU1") | |
#' @import rsdmx, dplyr | |
cod_sdmx <- function(rid, label = TRUE) { | |
require(rsdmx) | |
require(dplyr) | |
dsd <- readSDMX(providerId = "ISTAT", resource = "datastructure", | |
resourceId=rid) | |
comp <- slot(dsd, "datastructures") | |
comp <- slot(slot(comp, "datastructures")[[1]], "Components") | |
comp <- filter(as.data.frame(comp), component == "Dimension") | |
comp <- select(comp, component, conceptRef, codelist) | |
cod <- slot(dsd, "codelists") | |
if (label == TRUE) { | |
res <- list() | |
for (i in 1:nrow(comp)) { | |
res[[i]] <- as.data.frame(cod, codelistId = comp[i,3]) | |
res[[i]] <- tibble(concept = comp[i,2], | |
id = res[[i]]$id, label.it = res[[i]]$label.it, | |
label.en = res[[i]]$label.en) | |
} | |
res <- bind_rows(res) | |
list("campi" = comp, "codici" = res)} else comp | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment