When we request data from tables or series there is the possibility of filtering data on the fly using metadata information about the variables and their values that define the series.
Filtering data from tables
It is necessary to pass the argument filter
of the
get_data_table()
function, which is a list()
of variables and the values they take. There are two approaches to build
the filter depending on the table type.
Case one.
- The first step is to obtain the values of the groups (combo boxes) of the table that interest us to build the filter.
library(ineapir)
# Get metadata information of the table
metadata <- get_metadata_table_varval(idTable = 50902, validate = FALSE)
head(metadata,4)
#> Id Fk_Variable Nombre Codigo
#> 1 304092 762 Índice general 00
#> 2 304093 762 Alimentos y bebidas no alcohólicas 01
#> 3 304094 762 Bebidas alcohólicas y tabaco 02
#> 4 304095 762 Vestido y calzado 03
tail(metadata,4)
#> Id Fk_Variable Nombre Codigo
#> 14 83 3 Índice 0
#> 15 84 3 Variación mensual 1
#> 16 74 3 Variación anual 2
#> 17 87 3 Variación en lo que va de año 5
- With this information we build the filter as follows.
# The filter is a list()
filter <- list("3" = "74" , # variable id = 3, value id = 74
"762" = "304092" # variable id = 762, value id = 304092
)
# Request data using the filter
ipc <- get_data_table(idTable = 50902, filter = filter, unnest = TRUE,
tip = "A", nlast = 5, validate = FALSE)
ipc[,c("Nombre", "T3_Periodo", "Anyo", "Valor")]
#> Nombre T3_Periodo Anyo Valor
#> 1 Total Nacional. Índice general. Variación anual. M07 2024 2.8
#> 1.1 Total Nacional. Índice general. Variación anual. M06 2024 3.4
#> 1.2 Total Nacional. Índice general. Variación anual. M05 2024 3.6
#> 1.3 Total Nacional. Índice general. Variación anual. M04 2024 3.3
#> 1.4 Total Nacional. Índice general. Variación anual. M03 2024 3.2
Case two (pc-axis file)
- The first step is to obtain the metadata information from a table.
# Get metadata information of the table
metadata <- get_metadata_table_varval(idTable = "t20/e245/p08/l0/01001.px")
metadata
#> Nombre Codigo Variable.Nombre Variable.Codigo
#> 1 TOTAL EDADES totaledades edad (3 grupos de edad) edad3gruposdeedad
#> 2 TOTAL total Españoles/Extranjeros espanolesextranjeros
#> 3 Ambos sexos ambossexos Sexo sexo
#> 6 Hombres hombres Sexo sexo
#> 9 Mujeres mujeres Sexo sexo
#> 11 Españoles espanoles Españoles/Extranjeros espanolesextranjeros
#> 20 Extranjeros extranjeros Españoles/Extranjeros espanolesextranjeros
#> 29 % Extranjeros extranjeros~1 Españoles/Extranjeros espanolesextranjeros
#> 37 0-15 años 015anos edad (3 grupos de edad) edad3gruposdeedad
#> 73 16-64 años 1664anos edad (3 grupos de edad) edad3gruposdeedad
#> 109 65 y más 65ymas edad (3 grupos de edad) edad3gruposdeedad
# NOTE: for px tables we can use a filter.
metadata <- get_metadata_table_varval(idTable = "t20/e245/p08/l0/01001.px",
filter = list(sexo = "ambossexos"))
metadata
#> Nombre Codigo Variable.Nombre Variable.Codigo
#> 1 TOTAL EDADES totaledades edad (3 grupos de edad) edad3gruposdeedad
#> 2 TOTAL total Españoles/Extranjeros espanolesextranjeros
#> 3 Ambos sexos ambossexos Sexo sexo
#> 5 Españoles espanoles Españoles/Extranjeros espanolesextranjeros
#> 8 Extranjeros extranjeros Españoles/Extranjeros espanolesextranjeros
#> 11 % Extranjeros extranjeros~1 Españoles/Extranjeros espanolesextranjeros
#> 13 0-15 años 015anos edad (3 grupos de edad) edad3gruposdeedad
#> 25 16-64 años 1664anos edad (3 grupos de edad) edad3gruposdeedad
#> 37 65 y más 65ymas edad (3 grupos de edad) edad3gruposdeedad
- With this information we build the filter as follows.
# Build the filter with the codes of variables and values
filter <- list(sexo = "ambossexos",
espanolesextranjeros = "total",
edad3gruposdeedad = "totaledades"
)
# Request data using the filter
poblacion <- get_data_table(idTable = "t20/e245/p08/l0/01001.px", unnest = TRUE,
tip = "A", nlast = 5, filter = filter,
validate = FALSE)
poblacion
#> Nombre NombrePeriodo Valor
#> 1 TOTAL EDADES, TOTAL, Ambos sexos 2022 47475420
#> 2 TOTAL EDADES, TOTAL, Ambos sexos 2021 47385107
#> 3 TOTAL EDADES, TOTAL, Ambos sexos 2020 47450795
#> 4 TOTAL EDADES, TOTAL, Ambos sexos 2019 47026208
#> 5 TOTAL EDADES, TOTAL, Ambos sexos 2018 46722980
Case three (tpx file)
- The first step is to obtain the metadata information from a table.
# Get metadata information of the table
metadata <- get_metadata_table_varval(idTable = 33387)
metadata[grepl("^\\d{1}\\D+",metadata$Codigo),]
#> Nombre
#> 2 1. Biomasa
#> 29 2. Minerales metálicos (mineral en bruto)
#> 50 3. Minerales no metálicos
#> 106 4. Combustibles fósiles
#> Codigo Variable.Nombre Variable.Codigo
#> 2 1biomasa tipo de material tipodematerial
#> 29 2mineralesmetalicosmineralenbruto tipo de material tipodematerial
#> 50 3mineralesnometalicos tipo de material tipodematerial
#> 106 4combustiblesfosiles tipo de material tipodematerial
# NOTE: for tpx tables we can use a filter.
metadata <- get_metadata_table_varval(idTable = 33387,
filter = list(tipodematerial = "1biomasa"))
metadata[grepl("^\\d{1}\\D+",metadata$Codigo),]
#> Nombre Codigo Variable.Nombre Variable.Codigo
#> 1 1. Biomasa 1biomasa tipo de material tipodematerial
- With this information we build the filter as follows.
# Build the filter with the codes of variables and values
# A variable can take more than one value
filter <- list(tipodematerial = c("1biomasa", "2mineralesmetalicosmineralenbruto",
"3mineralesnometalicos", "4combustiblesfosiles")
)
# Request data using the filter
materiales <- get_data_table(idTable = 33387, unnest = TRUE, tip = "A",
nlast = 1, filter = filter, validate = FALSE)
materiales
#> Nombre NombrePeriodo Valor
#> 1 1. Biomasa 2022 (avance) 117875849
#> 2 2. Minerales metálicos (mineral en bruto) 2022 (avance) 18548154
#> 3 3. Minerales no metálicos 2022 (avance) 213803227
#> 4 4. Combustibles fósiles 2022 (avance) 115431
- There are tpx tables that contain variable ids and value ids. We can see that when we obtain the metadata information from a table.
# Get metadata information of the table using a filter.
# The filter is useful when the table contains a large number of series
# (for example, tables with a large territorial segmentation) which slows down
# the information retrieval.
metadata <- get_metadata_table_varval(idTable = 52056,
filter = list(NAC = "00"))
head(metadata[order(metadata$Variable.Id),],4)
#> Nombre Codigo Id Variable.Nombre Variable.Codigo Variable.Id
#> 6 Valor absoluto 11406 Tipo de dato 3
#> 12 Porcentaje 77 Tipo de dato 3
#> 4 Total 451 Sexo 18
#> 40 Hombres 1 452 Sexo 18
- In this case, we can use the ids instead of the codes to build the filter. To do this we add the alias ~id at the end of each id.
# In order to use the ids of variables and values we add the alias '~id'
filter = list("349~id" = "16473~id", # variable id = 349, value id = 16473
"916~id" = "391871~id", # variable id = 909, value id = 391455
"942~id" = "274282~id", # variable id = 942, value id = 274282
"999~id" = "391770~id", # variable id = 975, value id = 391438
"3~id" = "11406~id" # variable id = 3, value id = 11406
)
# Request data using the filter
explotaciones <- get_data_table(idTable = 52056, unnest = TRUE, tip = "A",
nlast = 1, filter = filter, validate = FALSE)
explotaciones
#> Nombre
#> 1 Total Nacional, Total tramos UTAT, Total mano de obra, Total, Nº explotaciones, Valor absoluto
#> 2 Total Nacional, Total tramos UTAT, Total mano de obra, Hombres, Nº explotaciones, Valor absoluto
#> 3 Total Nacional, Total tramos UTAT, Total mano de obra, Mujeres, Nº explotaciones, Valor absoluto
#> Valor
#> 1 894718
#> 2 757886
#> 3 356571
Filtering data from series
It is necessary to pass the argument filter
of the
get_data_series_filter()
function, which is a
list()
of variables and the values they take.
- The first step is to obtain the variables used in the operation to which the series belong.
# Variables used in the operation IPC
variables <- get_metadata_variables(operation = "IPC", validate = FALSE)
variables
#> Id Nombre Codigo
#> 1 3 Tipo de dato
#> 2 70 Comunidades y Ciudades Autónomas CCAA
#> 3 115 Provincias PROV
#> 4 269 Grupos especiales 2001
#> 5 270 Rúbricas 2001
#> 6 349 Totales Territoriales NAC
#> 7 544 Corrección de efectos
#> 8 762 Grupos ECOICOP
#> 9 763 Subgrupos ECOICOP
#> 10 764 Clases ECOICOP
#> 11 765 Subclases ECOICOP
- The second step is to obtain the values of the variables that interest us to build the filter.
# Values of the variable with id = 115
provincias <- get_metadata_values(operation = "IPC", variable = 115, validate = FALSE)
head(provincias)
#> Id Fk_Variable Nombre Codigo FK_JerarquiaPadres
#> 1 2 115 Araba/Álava 01 9012
#> 2 3 115 Albacete 02 9004
#> 3 4 115 Alicante/Alacant 03 9006
#> 4 5 115 Almería 04 8997
#> 5 6 115 Ávila 05 9003
#> 6 7 115 Badajoz 06 9007
# Values of the variable with id = 3
tipo <- get_metadata_values(operation = "IPC", variable = 3, validate = FALSE)
head(tipo)
#> Id Fk_Variable Nombre Codigo
#> 1 72 3 Dato base
#> 2 74 3 Variación anual
#> 3 83 3 Índice
#> 4 84 3 Variación mensual
#> 5 85 3 Media anual M
#> 6 86 3 Variación anual
# Values of the variable with id = 762
grupos <- get_metadata_values(operation = "IPC", variable = 762, validate = FALSE)
head(grupos, 4)
#> Id Fk_Variable Nombre Codigo
#> 1 304092 762 Índice general 00
#> 2 304093 762 Alimentos y bebidas no alcohólicas 01
#> 3 304094 762 Bebidas alcohólicas y tabaco 02
#> 4 304095 762 Vestido y calzado 03
#> FK_JerarquiaPadres
#> 1 NULL
#> 2 304092
#> 3 304092
#> 4 304092
# We can get all the values at once with the function get_metadata_series_varval
varval <- get_metadata_series_varval(operation = "IPC", validate = FALSE)
head(subset(varval, Fk_Variable == 115))
#> Id Fk_Variable Nombre Codigo
#> 33 2 115 Araba/Álava 01
#> 34 3 115 Albacete 02
#> 35 4 115 Alicante/Alacant 03
#> 36 5 115 Almería 04
#> 37 6 115 Ávila 05
#> 38 7 115 Badajoz 06
head(subset(varval, Fk_Variable == 3))
#> Id Fk_Variable Nombre Codigo
#> 1 72 3 Dato base
#> 2 74 3 Variación anual
#> 3 83 3 Índice
#> 4 84 3 Variación mensual
#> 5 85 3 Media anual M
#> 6 86 3 Variación anual
head(subset(varval, Fk_Variable == 762), 4)
#> Id Fk_Variable Nombre Codigo
#> 176 304092 762 Índice general 00
#> 177 304093 762 Alimentos y bebidas no alcohólicas 01
#> 178 304094 762 Bebidas alcohólicas y tabaco 02
#> 179 304095 762 Vestido y calzado 03
- With this information we build the filter as follows.
# The filter is a list()
filter <- list("115" = "2", # variable id = 115, value id = 2
"3" = "74" , # variable id = 3, value id = 74
"762" = "304092" # variable id = 762, value id = 304092
)
# Request data using the filter
ipc <- get_data_series_filter(operation = "IPC", filter = filter, periodicity = 1,
unnest = TRUE, tip = "A", validate = FALSE)
ipc[,c("Nombre", "T3_Periodo", "Anyo", "Valor")]
#> Nombre T3_Periodo Anyo Valor
#> 1 Araba/Álava. Índice general. Variación anual. Junio 2024 3.2
- A variable can take more than one value (valid to filter data from tables as well).
# The filter is a list()
filter <- list("115" = c("2" ,"3", "4"), # variable id = 115, values id 2, 3, 4
"3" = "74" , # variable id = 3, value id = 74
"762" = "304092" # variable id = 762, value id = 304092
)
# Request data using the filter
ipc <- get_data_series_filter(operation = "IPC", filter = filter, periodicity = 1,
unnest = TRUE, tip = "A", validate = FALSE)
ipc[,c("Nombre", "T3_Periodo", "Anyo", "Valor")]
#> Nombre T3_Periodo Anyo Valor
#> 1 Alicante/Alacant. Índice general. Variación anual. Junio 2024 3.5
#> 2 Albacete. Índice general. Variación anual. Junio 2024 3.6
#> 3 Araba/Álava. Índice general. Variación anual. Junio 2024 3.2
- A variable can take a empty character
""
to get all its possible values (valid to filter data from tables as well).
# The filter is a list()
filter <- list("115" = "", # variable id = 115, all values
"3" = "83" , # variable id = 3, value id = 83
"762" = "304092" # variable id = 762, value id = 304092
)
# Request data using the filter
ipc <- get_data_series_filter(operation = "IPC", filter = filter, periodicity = 1,
unnest = TRUE, tip = "A", validate = FALSE)
ipc[,c("Nombre", "T3_Periodo", "Anyo", "Valor")]
#> Nombre T3_Periodo Anyo Valor
#> 1 Salamanca. Índice general. Índice. Junio 2024 116.806
#> 2 Huelva. Índice general. Índice. Junio 2024 117.536
#> 3 Ciudad Real. Índice general. Índice. Junio 2024 117.987
#> 4 Alicante/Alacant. Índice general. Índice. Junio 2024 116.833
#> 5 Ourense. Índice general. Índice. Junio 2024 117.467
#> 6 Santa Cruz de Tenerife. Índice general. Índice. Junio 2024 116.432
#> 7 Coruña, A. Índice general. Índice. Junio 2024 116.580
#> 8 Valladolid. Índice general. Índice. Junio 2024 116.290
#> 9 Huesca. Índice general. Índice. Junio 2024 116.819
#> 10 Cádiz. Índice general. Índice. Junio 2024 116.573
#> 11 Málaga. Índice general. Índice. Junio 2024 117.558
#> 12 Teruel. Índice general. Índice. Junio 2024 116.683
#> 13 Albacete. Índice general. Índice. Junio 2024 117.757
#> 14 Rioja, La. Índice general. Índice. Junio 2024 116.618
#> 15 Pontevedra. Índice general. Índice. Junio 2024 117.721
#> 16 Melilla. Índice general. Índice. Junio 2024 117.681
#> 17 Cuenca. Índice general. Índice. Junio 2024 117.260
#> 18 Toledo. Índice general. Índice. Junio 2024 118.698
#> 19 Córdoba. Índice general. Índice. Junio 2024 117.040
#> 20 Ceuta. Índice general. Índice. Junio 2024 116.080
#> 21 Granada. Índice general. Índice. Junio 2024 116.711
#> 22 Ávila. Índice general. Índice. Junio 2024 118.431
#> 23 Tarragona. Índice general. Índice. Junio 2024 116.052
#> 24 Soria. Índice general. Índice. Junio 2024 116.474
#> 25 Valencia/València. Índice general. Índice. Junio 2024 115.604
#> 26 Guadalajara. Índice general. Índice. Junio 2024 117.864
#> 27 Burgos. Índice general. Índice. Junio 2024 116.353
#> 28 Lugo. Índice general. Índice. Junio 2024 117.577
#> 29 Gipuzkoa. Índice general. Índice. Junio 2024 116.088
#> 30 Almería. Índice general. Índice. Junio 2024 115.479
#> 31 Girona. Índice general. Índice. Junio 2024 116.352
#> 32 Lleida. Índice general. Índice. Junio 2024 117.923
#> 33 Zaragoza. Índice general. Índice. Junio 2024 115.922
#> 34 Zamora. Índice general. Índice. Junio 2024 117.954
#> 35 Castellón/Castelló. Índice general. Índice. Junio 2024 117.385
#> 36 Jaén. Índice general. Índice. Junio 2024 117.251
#> 37 Asturias. Índice general. Índice. Junio 2024 116.143
#> 38 Araba/Álava. Índice general. Índice. Junio 2024 115.220
#> 39 Badajoz. Índice general. Índice. Junio 2024 117.214
#> 40 Madrid. Índice general. Índice. Junio 2024 114.589
#> 41 Bizkaia. Índice general. Índice. Junio 2024 116.600
#> 42 Cantabria. Índice general. Índice. Junio 2024 116.004
#> 43 Palmas, Las. Índice general. Índice. Junio 2024 116.094
#> 44 Segovia. Índice general. Índice. Junio 2024 117.072
#> 45 Palencia. Índice general. Índice. Junio 2024 116.080
#> 46 Sevilla. Índice general. Índice. Junio 2024 116.515
#> 47 León. Índice general. Índice. Junio 2024 118.608
#> 48 Balears, Illes. Índice general. Índice. Junio 2024 116.734
#> 49 Barcelona. Índice general. Índice. Junio 2024 115.526
#> 50 Navarra. Índice general. Índice. Junio 2024 116.617
#> 51 Cáceres. Índice general. Índice. Junio 2024 115.384
#> 52 Murcia. Índice general. Índice. Junio 2024 116.888