Skip to contents

This guide explains how to create geo table for recoding and mapping geo codes. The table will be saved in a database file as specified in orgdata.geo option. See getOptions("orgdata.geo")

Recode Geo Codes

The function to create table for recoding geo codes is geo_recode(). The table consists of previous codes and their new codes whenever available. For instance, to get all codes for kommuner from 2017 to 2022. The table will list all the codes for kommuner since 2017 in columname oldCode while their current codes ie. 2022, is in column currentCode. Any geo codes that have changed since 2017 will have their new codes in currentCode column. For geo codes that have not changed since 2017 will have the value as NA in the oldCode column.

Use argument write = TRUE to save it in the database. The table in the database will be named as kommune2022 if the data consist of geo codes up to 2022, ie. argument to = 2022 and type = kommune were used. If the table already exists in the database and you just want to add a new dataset to the table, you could use argument append = TRUE, but appending should only be used for control purposes and not for production as it might create duplication in oldCode column.

Please check the documentation for details with help("geo_recode").

library(orgdata)
dt <- geo_recode(type = "kommune", from = 2018, to = 2022)
dt[1:5]
oldCode              oldName currentCode           newName changeOccurred      batch
1:    <NA>              <NA>        0301              Oslo           2022 2022-03-15
2:    <NA>              <NA>        1101         Eigersund           2022 2022-03-15
3:    1141            Finnøy        1103         Stavanger           2020 2022-03-15
4:    1142          Rennesøy        1103         Stavanger           2020 2022-03-15
5:    <NA>              <NA>        1106         Haugesund           2022 2022-03-15

Mapping Geo Codes

The function to create geo mapping is geo_map(). The default table name to be created in the database is tblGeo. It consists of all the geo granularities (levels) of the selected year. The lowest level is enumeration area codes or grunnkrets.

Arguments write and append can be used when calling geo_map() function. However in most cases, you will only need to use append = TRUE, ie. to append the data to the existing tblGeo in the database.

DT <- geo_map(year = 2022)
DT[sample(1:.N, 5)]
code                      name validTo      level grunnkrets kommune fylke bydel      batch
1: 30470104 Søndre Simostranda    2022 grunnkrets   30470104    3047    30  <NA> 2022-03-15
2: 11140110           Vikeså 2    2022 grunnkrets   11140110    1114    11  <NA> 2022-03-15
3: 30020408           Ørehavna    2022 grunnkrets   30020408    3002    30  <NA> 2022-03-15
4: 46350304          Hjartholm    2022 grunnkrets   46350304    4635    46  <NA> 2022-03-15
5: 42160109     Birkeland Nord    2022 grunnkrets   42160109    4216    42  <NA> 2022-03-15

Mapping geo codes for multiple years

To create a mapping table spanning multiple years, use the function geo_map_multi(). This is a wrapper around geo_map() which generates a multi-year table.

DT <- geo_map_multi(from = 2022, to = 2024)

Merge Geo Codes

Both geo_recode() and geo_map() fetch the data via API available from SSB. To merge geo codes that aren’t available via API can be done with the function geo_merge(). These codes can be added to tblGeo from the database, or to a locally generated table generated with geo_map_multi().

The data to merge can be in any format that is accepted by read_file() function. The new data must have a column with geo codes corresponding to a column in tblGeo and a column with the new geo codes to be added. Optionally a column with names for the new geo level. For instance if a new dataset from 2024 has levekaar codes in columname code_levekaar and these levekaar codes are derived from grunnkrets codes with columname var01 and levekaar names in var02, then the specification for the arguments will be as the example below.

The complete table can be written to the geo database, and the default is to write to tblGeo.

Please read the documentation for details on arguments that can be used with help("geo_merge") or ?geo_merge.

dd <- geo_merge(id.table = "grunnkrets",
                id.file = "Grunnkrets_kode",
                geo.col = "Delbydel_kode",
                geo.level = "levekaar",
                geo.name = "Delbydel_navn",
                file = "path/to/file.csv",
                localtable = NULL, 
                year = 2024, 
                table.name = "tblGeo")

To generate a local table and merge new geo-codes you can use the following code:

DT <- geo_map_multi(from = 2022, to = 2024)

dd <- geo_merge(id.table = "grunnkrets",
                id.file = "Grunnkrets_kode",
                geo.col = "Delbydel_kode",
                geo.level = "levekaar",
                geo.name = "Delbydel_navn",
                file = "path/to/file.csv",
                localtable = DT, 
                year = 2024, 
                table.name = "tblGeo")