Skip to contents

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.

  1. 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
  1. 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.         M05 2024   3.6
#> 1.1 Total Nacional. Índice general. Variación anual.         M04 2024   3.3
#> 1.2 Total Nacional. Índice general. Variación anual.         M03 2024   3.2
#> 1.3 Total Nacional. Índice general. Variación anual.         M02 2024   2.8
#> 1.4 Total Nacional. Índice general. Variación anual.         M01 2024   3.4

Case two (pc-axis file)

  1. 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
  1. 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)

  1. 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
  1. 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
  1. 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
  1. 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.

  1. 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
  1. 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
  1. 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.        Mayo 2024   3.6
  • 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.        Mayo 2024   3.8
#> 2         Albacete. Índice general. Variación anual.        Mayo 2024   3.9
#> 3      Araba/Álava. Índice general. Variación anual.        Mayo 2024   3.6
  • 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.        Mayo 2024 116.489
#> 2                  Huelva. Índice general. Índice.        Mayo 2024 117.277
#> 3             Ciudad Real. Índice general. Índice.        Mayo 2024 117.655
#> 4        Alicante/Alacant. Índice general. Índice.        Mayo 2024 116.364
#> 5                 Ourense. Índice general. Índice.        Mayo 2024 117.210
#> 6  Santa Cruz de Tenerife. Índice general. Índice.        Mayo 2024 116.099
#> 7               Coruña, A. Índice general. Índice.        Mayo 2024 116.251
#> 8              Valladolid. Índice general. Índice.        Mayo 2024 115.919
#> 9                  Huesca. Índice general. Índice.        Mayo 2024 116.638
#> 10                  Cádiz. Índice general. Índice.        Mayo 2024 116.242
#> 11                 Málaga. Índice general. Índice.        Mayo 2024 117.075
#> 12                 Teruel. Índice general. Índice.        Mayo 2024 116.092
#> 13               Albacete. Índice general. Índice.        Mayo 2024 117.424
#> 14              Rioja, La. Índice general. Índice.        Mayo 2024 116.203
#> 15             Pontevedra. Índice general. Índice.        Mayo 2024 117.285
#> 16                Melilla. Índice general. Índice.        Mayo 2024 117.095
#> 17                 Cuenca. Índice general. Índice.        Mayo 2024 116.880
#> 18                 Toledo. Índice general. Índice.        Mayo 2024 118.544
#> 19                Córdoba. Índice general. Índice.        Mayo 2024 116.843
#> 20                  Ceuta. Índice general. Índice.        Mayo 2024 115.552
#> 21                Granada. Índice general. Índice.        Mayo 2024 116.531
#> 22                  Ávila. Índice general. Índice.        Mayo 2024 118.009
#> 23              Tarragona. Índice general. Índice.        Mayo 2024 115.485
#> 24                  Soria. Índice general. Índice.        Mayo 2024 116.185
#> 25      Valencia/València. Índice general. Índice.        Mayo 2024 115.140
#> 26            Guadalajara. Índice general. Índice.        Mayo 2024 117.282
#> 27                 Burgos. Índice general. Índice.        Mayo 2024 115.963
#> 28                   Lugo. Índice general. Índice.        Mayo 2024 117.034
#> 29               Gipuzkoa. Índice general. Índice.        Mayo 2024 115.754
#> 30                Almería. Índice general. Índice.        Mayo 2024 115.430
#> 31                 Girona. Índice general. Índice.        Mayo 2024 115.667
#> 32                 Lleida. Índice general. Índice.        Mayo 2024 117.145
#> 33               Zaragoza. Índice general. Índice.        Mayo 2024 115.420
#> 34                 Zamora. Índice general. Índice.        Mayo 2024 117.773
#> 35     Castellón/Castelló. Índice general. Índice.        Mayo 2024 116.969
#> 36                   Jaén. Índice general. Índice.        Mayo 2024 116.842
#> 37               Asturias. Índice general. Índice.        Mayo 2024 115.410
#> 38            Araba/Álava. Índice general. Índice.        Mayo 2024 114.908
#> 39                Badajoz. Índice general. Índice.        Mayo 2024 116.919
#> 40                 Madrid. Índice general. Índice.        Mayo 2024 114.149
#> 41                Bizkaia. Índice general. Índice.        Mayo 2024 116.165
#> 42              Cantabria. Índice general. Índice.        Mayo 2024 115.759
#> 43            Palmas, Las. Índice general. Índice.        Mayo 2024 115.723
#> 44                Segovia. Índice general. Índice.        Mayo 2024 116.669
#> 45               Palencia. Índice general. Índice.        Mayo 2024 115.811
#> 46                Sevilla. Índice general. Índice.        Mayo 2024 116.184
#> 47                   León. Índice general. Índice.        Mayo 2024 118.305
#> 48         Balears, Illes. Índice general. Índice.        Mayo 2024 116.027
#> 49              Barcelona. Índice general. Índice.        Mayo 2024 114.971
#> 50                Navarra. Índice general. Índice.        Mayo 2024 116.080
#> 51                Cáceres. Índice general. Índice.        Mayo 2024 114.945
#> 52                 Murcia. Índice general. Índice.        Mayo 2024 116.620