TL, DR
rsdmx
is a great R package to access data via SDMX. Sometimes the configuration of parameters to access the data may not be intuitive. Here a quick note from few hours spent trying to access data from ISTAT.
Install and import rsdmx
This is easily achieved opening R or Rstudio and calling:
install.packages("rsdmx")
library(rsdmx)
For convenience, in the following code snippets I will assume you have imported the various tidyverse
packages with:
library(tidyverse)
Check data providers
as.data.frame(getSDMXServiceProviders())
Get a list of flows from a provider
istat.flows <- readSDMX(providerId = "ISTAT", resource = "dataflow") %>%
as.data.frame()
istat.flows %>%
filter(str_detect(Name.it, "Prezzi"))
istat.flows %>%
filter(str_detect(id, "168_760"))
Get parameters for filtering a flow
readSDMX(providerId = "ISTAT", resource = "data",
flowRef = "168_760",
dsd = T) %>% as.data.frame() %>% names()
Get data and metadata
This can return huge objects…use with care. The relevant parameter is dsd = T
.
official_sdmx <- readSDMX(providerId = "ISTAT", resource = "data",
flowRef = "168_760",
key = list("M", "IT", "41", "4", "0733", NULL, NULL, NULL),
dsd = T)
Get data only
In most cases that’s what you want.
official_sdmx_a <- readSDMX(providerId = "ISTAT", resource = "data",
flowRef = "168_760",
key = list("M", "IT", "41", "4", "0733", NULL, NULL, NULL),
dsd = F)
official_sdmx_b <- readSDMX(providerId = "ISTAT", resource = "data",
flowRef = "168_760",
key = "M.IT.41.4.0733...",
dsd = F,
start = "2023-01-01")
Arrange the data
Just make a dataframe or a tibble out of it, using the labels from SDMX.
as.data.frame(official_sdmx_a,labels = TRUE)
as_tibble(official_sdmx)
Or you can get a xts
object:
library(xts)
xts_data <- as_tibble(official_sdmx) %>%
# Convert obsTime to Date (assuming it's year-month format)
mutate(obsTime = as.Date(paste0(obsTime, "-01"))) %>% # Adds day to make full date
# Create xts object
xts(x = .$obsValue, order.by = .$obsTime)
Related links
Do you like our content? Check more of our posts in our blog!