rOpenGov / iotables

Importing and Manipulating Symmetric Input-Output Tables
https://iotables.dataobservatory.eu
Other
21 stars 8 forks source link

iotabel_get is not working with naio_10_cp1750 #28

Closed LucaPavanIdea closed 2 months ago

LucaPavanIdea commented 2 months ago

I downloaded the data in this way

library(tidyverse) library(data.table) library(openxlsx) library(readxl) library(eurostat) library(iotables)

user <- Sys.info()[["user"]]

path <- paste0(dirname(rstudioapi::getActiveDocumentContext()$path), "/") path_dati <- paste0(path, "dati")

naio_10_cp1750 <- iotables_download( "naio_10_cp1750", #SIOT data_directory = path_dati)

gc()

naio_10_cp1750 <- naio_10_cp1750 %>% dplyr::filter ( geo == "IT") %>% dplyr::filter ( year == 2019)

emp_it <- employment_get(geo = "IT", year = "2019", sex = "Total", age = "Y_GE15", labelling = "prod_na", data_directory = path_dati, force_download = TRUE)

save (naio_10_cp1750, emp_it, file = file.path(path_dati, 'naio_10_industry_x_industry.rda'))

I then use iotable_get

load(file = paste0(path_dati, "/naio_10_industry_x_industry.rda"))

it_io <- iotable_get ( labelled_io_data = naio_10_cp1750, source = "naio_10_cp1750", geo = "IT", year = 2019, unit = "MIO_EUR", stk_flow = "TOTAL", labelling = "short" )

Error in dplyr::mutate(): ℹ In argument: prod_na = forcats::fct_reorder(prod_na, as.numeric(row_order)). Caused by error in lvls_reorder(): ! idx must contain one integer for each level of f Run rlang::last_trace() to see where the error occurred.

LucaPavanIdea commented 2 months ago

The problem was here

metadata_rows <- getdata (metadata) %>% #tables that follow prod_ind vocabulary dplyr::filter ( variable == "prod_na") %>% dplyr::rename ( prod_na = code) %>% dplyr::rename ( prod_na_lab = label ) %>% dplyr::rename ( row_order = numeric_label ) %>% dplyr::rename ( iotables_row = iotables_label )

The label for the code J59_60 for group == "Industry" differs for the variable in "induse" ("Motion picture, video, television programme production; programming and broadcasting activities) and variable "prod_na" (Publishing, motion picture, video, television programme production; sound recording, programming and broadcasting activities).

To solve it, I just change that:

metadata_rows <- getdata (metadata) %>% #tables that follow prod_ind vocabulary dplyr::mutate ( label = ifelse(code == "J59_60", label[code == "J59_60" & variable == "induse"], label)) %>% dplyr::filter ( variable == "prod_na") %>% dplyr::rename ( prod_na = code) %>% dplyr::rename ( prod_na_lab = label ) %>% dplyr::rename ( row_order = numeric_label ) %>% dplyr::rename ( iotables_row = iotables_label )