| Title: | Structural Data for Norway |
|---|---|
| Description: | Datasets relating to population in municipalities, municipality/county matching, and how different municipalities have merged/redistricted over time from 2006 to 2024. |
| Authors: | Richard Aubrey White [aut, cre] (ORCID: <https://orcid.org/0000-0002-6747-1726>), Chi Zhang [aut] (ORCID: <https://orcid.org/0000-0003-0501-5909>), Folkehelseinstituttet [cph] |
| Maintainer: | Richard Aubrey White <hello@rwhite.no> |
| License: | MIT + file LICENSE |
| Version: | 2026.8.21 |
| Built: | 2026-09-26 06:28:42 UTC |
| Source: | https://github.com/niphr/csdata |
Derives the geographic granularity label from the location_code column.
Adds the label as a new granularity_geo column, and changes x in place.
When location_reference is NULL, the granularity comes from the location
code prefix. When you supply a reference table, the granularity comes from
that table.
add_granularity_geo_to_data_set(x, location_reference = NULL)add_granularity_geo_to_data_set(x, location_reference = NULL)
x |
A data.table with a column named |
location_reference |
A data.table with columns |
x, invisibly, with the granularity_geo column added or updated.
vignette("csdata", package = "csdata"), which calls this
function on a worked example. location_code_to_granularity_geo()
returns the same labels as a vector instead of writing them onto x.
Other data set column adders:
add_iso3_to_data_set()
library(data.table) data <- data.table(location_code = c("nation_nor", "county_nor03", "blah")) csdata::add_granularity_geo_to_data_set(data) print(data) library(data.table) data <- data.table(location_code = c("nation_nor", "county_nor03", "blah")) csdata::add_granularity_geo_to_data_set(data, location_reference = csdata::nor_locations_names()) print(data)library(data.table) data <- data.table(location_code = c("nation_nor", "county_nor03", "blah")) csdata::add_granularity_geo_to_data_set(data) print(data) library(data.table) data <- data.table(location_code = c("nation_nor", "county_nor03", "blah")) csdata::add_granularity_geo_to_data_set(data, location_reference = csdata::nor_locations_names()) print(data)
Adds an iso3 column that holds the ISO 3166-1 alpha-3 country code, and
changes x in place. csdata carries Norwegian data only, so the value is
"nor" on every row. The function does not read the contents of x, so x
gains the column even when it has no location_code column.
add_iso3_to_data_set(x)add_iso3_to_data_set(x)
x |
A data.table. Only a data.table method exists, so any other class raises "no applicable method". |
x, invisibly, with a new iso3 column holding "nor" on every
row.
No vignette covers this function. location_code_to_iso3() returns
the same values as a vector instead of writing them onto x.
Other data set column adders:
add_granularity_geo_to_data_set()
library(data.table) data <- data.table(location_code = c("nation_nor", "county_nor03", "blah")) csdata::add_iso3_to_data_set(data) print(data)library(data.table) data <- data.table(location_code = c("nation_nor", "county_nor03", "blah")) csdata::add_iso3_to_data_set(data) print(data)
An environment that stores package-level configuration variables.
Change it with set_config(). Read it directly, for example
config$border_nor.
configconfig
Available variables:
border_nor (default 2024): the border year that selects which
Norwegian geographic datasets a function reads. Valid values: 2024.
border_nor supplies the default border argument of
nor_locations_names(), nor_locations_redistricting(),
nor_locations_hierarchy_from_to(), nor_population_by_age_cats() and
nor_population_by_sex_age_cats(). No other exported function reads it.
No vignette covers this object.
print(ls(csdata::config)) for(i in names(csdata::config)){ cat(i, ":", csdata::config[[i]], "\n") }print(ls(csdata::config)) for(i in names(csdata::config)){ cat(i, ":", csdata::config[[i]], "\n") }
Extracts the geographic granularity label from one or more location codes.
When location_reference is NULL, the granularity comes from the
lowercase alphabetic prefix of the location code (e.g. "county_nor03" ->
"county"). The prefix "norge" is the one special case, and it gives
"nation". When you supply a reference table, the granularity comes from
that table.
location_code_to_granularity_geo(x, location_reference = NULL)location_code_to_granularity_geo(x, location_reference = NULL)
x |
A character vector of location codes, or a data.table with a
column named |
location_reference |
A data.table with columns |
A character vector of the corresponding granularity_geo values.
It has the same length as x, or as many elements as x has rows when
x is a data.table. A code with no leading lowercase letters yields NA.
An unmatched code also yields NA when you supply location_reference.
The length guarantee has one exception. location_reference is joined on
location_code, so a code that appears more than once in the reference
contributes one element per matching row. nor_locations_names() holds
one such code today, "lab_nor084467", which two laboratories share.
vignette("csdata", package = "csdata"), which calls this
function on a worked example.
vignette("locations_norway", package = "csdata") tabulates the
location_code values returned by nor_locations_names().
Other location code converters:
location_code_to_iso3()
csdata::location_code_to_granularity_geo(c("nation_nor", "county_nor03", "municip_nor0301")) # a code with no lowercase prefix gives NA csdata::location_code_to_granularity_geo(c("nation_nor", "", NA)) library(data.table) dt <- data.table(location_code = c("nation_nor", "county_nor03")) csdata::location_code_to_granularity_geo(dt) # looked up against a reference table instead of parsed from the prefix csdata::location_code_to_granularity_geo( c("nation_nor", "county_nor03", "blah"), location_reference = csdata::nor_locations_names() )csdata::location_code_to_granularity_geo(c("nation_nor", "county_nor03", "municip_nor0301")) # a code with no lowercase prefix gives NA csdata::location_code_to_granularity_geo(c("nation_nor", "", NA)) library(data.table) dt <- data.table(location_code = c("nation_nor", "county_nor03")) csdata::location_code_to_granularity_geo(dt) # looked up against a reference table instead of parsed from the prefix csdata::location_code_to_granularity_geo( c("nation_nor", "county_nor03", "blah"), location_reference = csdata::nor_locations_names() )
Returns the ISO 3166-1 alpha-3 country code for each location code. csdata
carries Norwegian data only, so the implementation returns "nor" for every
element without inspecting its value. A code from another country, or a
string that is not a location code at all, also returns "nor".
location_code_to_iso3(x)location_code_to_iso3(x)
x |
A character vector of location codes, or a data.table with a
column named |
A character vector of "nor". It has the same length as x, or
as many elements as x has rows when x is a data.table.
No vignette covers this function.
vignette("locations_norway", package = "csdata") tabulates the
location_code values returned by nor_locations_names().
Other location code converters:
location_code_to_granularity_geo()
csdata::location_code_to_iso3(c("nation_nor", "county_nor03", "municip_nor0301")) # the input value is not inspected csdata::location_code_to_iso3(c("county_nor03", "xyz")) library(data.table) dt <- data.table(location_code = c("nation_nor", "county_nor03")) csdata::location_code_to_iso3(dt)csdata::location_code_to_iso3(c("nation_nor", "county_nor03", "municip_nor0301")) # the input value is not inspected csdata::location_code_to_iso3(c("county_nor03", "xyz")) library(data.table) dt <- data.table(location_code = c("nation_nor", "county_nor03")) csdata::location_code_to_iso3(dt)
A named list of Norwegian special characters as unicode strings,
for use where literal non-ASCII characters are inconvenient.
Elements: AA/aa (Aa/aa), OE/oe (Oe/oe), AE/ae (Ae/ae).
Each element is a single character.
nbnb
No vignette covers this object.
Other unicode character lists:
se
print(csdata::nb) csdata::nb$AA # uppercase Aaprint(csdata::nb) csdata::nb$AA # uppercase Aa
Returns a mapping between two geographic levels in Norway. Both from and
to accept character vectors. The function then returns every requested
combination, joined into a single data.table.
nor_locations_hierarchy_from_to( from, to, include_to_name = FALSE, border = csdata::config$border_nor )nor_locations_hierarchy_from_to( from, to, include_to_name = FALSE, border = csdata::config$border_nor )
from |
Character vector. The source geographic granularity. One or more
of: |
to |
Character vector. The target geographic granularity. Same valid
values as |
include_to_name |
Logical. If |
border |
Integer. The geographic border year. Valid values: |
A combination that the bundled hierarchy table cannot express returns a
zero-row data.table rather than an error. A from or to value of
"baregion" always does this, because the bundled table carries no
BA-region codes. Use nor_locations_names() to list the BA-regions
themselves.
A data.table with columns:
Location code at the from granularity level.
Location code at the to granularity level.
Name of the to location (only present when
include_to_name = TRUE).
The table has no key set, and a combination the bundled hierarchy cannot express yields zero rows.
vignette("csdata", package = "csdata"), which calls
nor_locations_hierarchy_from_to() on a worked example.
vignette("locations_norway", package = "csdata") tabulates the
location_code values returned by nor_locations_names(), which are the
values from_code and to_code are drawn from.
csdata::nor_locations_hierarchy_from_to(from = "wardoslo", to = "county") nrow(csdata::nor_locations_hierarchy_from_to(from = "municip", to = "county")) csdata::nor_locations_hierarchy_from_to( from = c("municip", "county"), to = "georegion", include_to_name = TRUE )[1:5] # baregion is accepted but the bundled hierarchy holds no BA-region codes nrow(csdata::nor_locations_hierarchy_from_to(from = "municip", to = "baregion"))csdata::nor_locations_hierarchy_from_to(from = "wardoslo", to = "county") nrow(csdata::nor_locations_hierarchy_from_to(from = "municip", to = "county")) csdata::nor_locations_hierarchy_from_to( from = c("municip", "county"), to = "georegion", include_to_name = TRUE )[1:5] # baregion is accepted but the bundled hierarchy holds no BA-region codes nrow(csdata::nor_locations_hierarchy_from_to(from = "municip", to = "baregion"))
Returns a data.table of Norwegian geographic units with their location codes, display names, and presentation metadata. Coverage includes nation, counties, municipalities, city districts (Oslo, Bergen, Stavanger, Trondheim), BA-regions, and lab regions, plus placeholder codes for not-mainland and unknown locations. The returned table has no key set.
nor_locations_names(border = csdata::config$border_nor)nor_locations_names(border = csdata::config$border_nor)
border |
Integer. The geographic border year that selects which
administrative boundaries the function uses. Valid values: |
A data.table with one row per geographic unit and the columns:
Location code (e.g. "nation_nor", "county_nor03").
Full location name.
Abbreviated name, NA on most rows. The
nation and the 5 georegions carry one letter. The 15 counties carry
three letters. The 15 Oslo city districts and the 2 extra Oslo wards
carry a display name of 4 to 13 characters. On some rows that display
name is identical to location_name. It is NA for municipalities,
BA-regions, lab regions, and the Bergen, Stavanger and Trondheim city
districts.
Location name with a parenthetical description of geographic level (Norwegian Bokmal).
Name suitable for use in file names, retaining Norwegian characters.
Name suitable for use in file names, with Norwegian characters replaced by ASCII equivalents.
Integer giving the preferred presentation order.
Geographic granularity: one of "nation",
"georegion", "mtregion", "county", "municip", "baregion",
"wardoslo", "wardbergen", "wardstavanger", "wardtrondheim",
"extrawardoslo", "lab", "notmainlandcounty",
"notmainlandmunicip", "missingcounty", "missingmunicip",
"missingwardoslo", "missingwardbergen", "missingwardstavanger",
"missingwardtrondheim".
https://no.wikipedia.org/wiki/Liste_over_norske_kommunenummer
vignette("locations_norway", package = "csdata") prints the
location_code and location_name_description_nb columns of this table in
full. vignette("csdata", package = "csdata") summarises the table by
granularity_geo and states which name column to use where.
d <- nor_locations_names() head(d) d[granularity_geo == "county"]d <- nor_locations_names() head(d) d[granularity_geo == "county"]
Returns a data.table of weighting factors used to convert historical data recorded under old administrative boundaries to the 2024 borders. Each row maps an original location code (as of a given calendar year) to the current location code, with a proportional weighting.
nor_locations_redistricting(border = csdata::config$border_nor)nor_locations_redistricting(border = csdata::config$border_nor)
border |
Integer. The target geographic border year. Valid values:
|
A data.table with columns:
Location code under the target border year.
Each of the 421 distinct values also appears in
nor_locations_names().
Location code as it existed in calyear.
Of the 941 distinct values, 520 are retired codes that
nor_locations_names() no longer lists.
The calendar year to which location_code_original
applies.
Proportional weight to apply when you aggregate from
the original location to the current location. Values sum to 1 within
each location_code_original / calyear group, on all 40,819 of
those groups. A row spread across several current locations is
therefore split, not duplicated. Values need not sum to 1 within a
location_code_current / calyear group: 4,334 of those 35,160
groups do not.
Geographic granularity: one of "nation",
"county", "municip", "wardbergen", "wardoslo",
"wardstavanger", "wardtrondheim", "extrawardoslo",
"missingwardbergen", "missingwardoslo", "missingwardstavanger",
"missingwardtrondheim", "notmainlandcounty",
"notmainlandmunicip", "missingcounty", "missingmunicip".
Statistics Norway (SSB) municipal reform documentation.
No vignette covers this function.
vignette("locations_norway", package = "csdata") tabulates the
location_code values returned by nor_locations_names(), which are the
values location_code_current is drawn from.
d <- csdata::nor_locations_redistricting() head(d) d[calyear == 2019 & granularity_geo == "municip"]d <- csdata::nor_locations_redistricting() head(d) d[calyear == 2019 & granularity_geo == "municip"]
Aggregates the bundled Norwegian population dataset (from Statistics Norway)
into caller-defined age bands. The underlying data covers every integer age
from 0 to 105 at national, georegion, county, municipality, and
city-district level. Not-mainland and unknown locations carry the single
placeholder age -99 instead. include_total counts that age, and a band
such as 0:105 does not.
nor_population_by_age_cats( cats = NULL, include_total = TRUE, include_9999 = FALSE, border = csdata::config$border_nor )nor_population_by_age_cats( cats = NULL, include_total = TRUE, include_9999 = FALSE, border = csdata::config$border_nor )
cats |
A named or unnamed list of integer vectors that give the age
values to include in each category. Each vector element defines one age
band. If a list element is named, that name becomes the |
include_total |
Logical. If |
include_9999 |
Logical. If |
border |
Integer. The geographic border year. Valid values: |
A data.table with columns:
Geographic granularity level.
Location code.
Age category label, as derived from cats names or
auto-generated, plus "total" if include_total = TRUE.
Sex. Always "total", because this function filters the
bundled dataset to the sex total. The dataset itself also holds
"male" and "female"; reach those with
nor_population_by_sex_age_cats().
Calendar year.
Population count as of 1 January of calyear.
Logical. TRUE if the value was imputed.
vignette("population_norway", package = "csdata") plots the
national series and tabulates pop_jan1_n by location and calendar year
from this function. vignette("csdata", package = "csdata") gives the
csverse coding rules for the age and sex columns.
Other population data:
nor_population_by_sex_age_cats()
# data.table's default multi-threading pushes this example past CRAN's # CPU-to-elapsed limit, so pin it to one thread and restore afterwards. old_threads <- data.table::getDTthreads() data.table::setDTthreads(1) # Default: return total population only d <- nor_population_by_age_cats() print(head(d[granularity_geo == "nation"])) # Named age bands d2 <- nor_population_by_age_cats( cats = list("children" = 0:17, "working_age" = 18:66, "elderly" = 67:105), include_total = TRUE ) print(d2[granularity_geo == "nation" & calyear == 2024]) data.table::setDTthreads(old_threads)# data.table's default multi-threading pushes this example past CRAN's # CPU-to-elapsed limit, so pin it to one thread and restore afterwards. old_threads <- data.table::getDTthreads() data.table::setDTthreads(1) # Default: return total population only d <- nor_population_by_age_cats() print(head(d[granularity_geo == "nation"])) # Named age bands d2 <- nor_population_by_age_cats( cats = list("children" = 0:17, "working_age" = 18:66, "elderly" = 67:105), include_total = TRUE ) print(d2[granularity_geo == "nation" & calyear == 2024]) data.table::setDTthreads(old_threads)
Aggregates the Norwegian population into custom age categories, split by sex (male/female) and optionally the total of both sexes.
nor_population_by_sex_age_cats( cats = NULL, include_total_age = TRUE, include_total_sex = TRUE, include_9999 = FALSE, border = csdata::config$border_nor )nor_population_by_sex_age_cats( cats = NULL, include_total_age = TRUE, include_total_sex = TRUE, include_9999 = FALSE, border = csdata::config$border_nor )
cats |
A list of vectors that give the age values to include in each category. |
include_total_age |
Logical. If |
include_total_sex |
Logical. If |
include_9999 |
Logical. If |
border |
Integer. The Norwegian geographic border year. Valid values:
|
Statistics Norway provides no sex breakdown for Svalbard and Jan Mayen
(notmainlandcounty_nor21, notmainlandcounty_nor22,
notmainlandmunicip_nor2100, notmainlandmunicip_nor2200). On those four
codes pop_jan1_n is NA on the male and female rows, and the total
row holds the real count. For the unknown-location codes
(missingcounty_nor99, missingmunicip_nor9999) pop_jan1_n is NA on all
three sex rows, total included.
When include_total_sex = TRUE, the output holds male, female and
total in long format. A sum of pop_jan1_n across all sex values
therefore double-counts. Filter to a single sex, or sum only the
male/female components.
A data.table containing the following columns:
granularity_geo
location_code
age (as specified in the argument "cats")
sex ("male", "female", and "total" if include_total_sex)
calyear
pop_jan1_n
imputed
No vignette covers this function.
vignette("population_norway", package = "csdata") uses
nor_population_by_age_cats(), which returns the sex total only.
vignette("csdata", package = "csdata") gives the csverse coding
rules for the age and sex columns.
Other population data:
nor_population_by_age_cats()
# data.table's default multi-threading pushes this example past CRAN's # CPU-to-elapsed limit, so pin it to one thread and restore afterwards. old_threads <- data.table::getDTthreads() data.table::setDTthreads(1) d <- nor_population_by_sex_age_cats(cats = list(c(1:10), c(11:20))) print(d[location_code == "nation_nor" & calyear == 2024]) data.table::setDTthreads(old_threads)# data.table's default multi-threading pushes this example past CRAN's # CPU-to-elapsed limit, so pin it to one thread and restore afterwards. old_threads <- data.table::getDTthreads() data.table::setDTthreads(1) d <- nor_population_by_sex_age_cats(cats = list(c(1:10), c(11:20))) print(d[location_code == "nation_nor" & calyear == 2024]) data.table::setDTthreads(old_threads)
A named list of Swedish special characters as unicode strings,
for use where literal non-ASCII characters are inconvenient.
Elements: OE/oe (Oe/oe), AE/ae (Ae/ae). Each element is a single
character. These four names are spelled the same way in nb, so code that
indexes se by name also works against nb.
sese
No vignette covers this object.
Other unicode character lists:
nb
print(csdata::se) csdata::se$oe # lowercase oeprint(csdata::se) csdata::se$oe # lowercase oe
Updates border_nor in the config environment. Call this at the start of
a script to change the default border argument of
nor_locations_names(), nor_locations_redistricting(),
nor_locations_hierarchy_from_to(), nor_population_by_age_cats() and
nor_population_by_sex_age_cats().
set_config(border_nor = NULL)set_config(border_nor = NULL)
border_nor |
Integer. The Norwegian geographic border year to use as
the default. Valid values: |
Invisibly, the value assigned to config$border_nor, or NULL
when border_nor is NULL. The function exists for its side effect on
config.
No vignette covers this function.
old <- csdata::config$border_nor csdata::set_config(border_nor = 2024) csdata::config$border_nor # 2024 csdata::set_config(border_nor = old) # restoreold <- csdata::config$border_nor csdata::set_config(border_nor = 2024) csdata::config$border_nor # 2024 csdata::set_config(border_nor = old) # restore