| 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>) |
| Maintainer: | Richard Aubrey White <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 2026.7.2 |
| Built: | 2026-07-03 03:05:55 UTC |
| Source: | https://github.com/cran/csdata |
Derives the geographic granularity label from the location_code column
and adds it as a new granularity_geo column, modifying x in place.
When location_reference is NULL the granularity is inferred from the
location code prefix; when a reference table is supplied, it is looked up
directly.
add_granularity_geo_to_data_set(x, location_reference = NULL)add_granularity_geo_to_data_set(x, location_reference = NULL)
x |
A data.table containing a column named |
location_reference |
A data.table with columns |
x, invisibly, with the granularity_geo column added or updated.
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)
Derives the ISO 3166-1 alpha-3 country code from the location_code column
and adds it as a new column, modifying x in place. Currently all Norwegian
location codes map to "nor".
add_iso3_to_data_set(x)add_iso3_to_data_set(x)
x |
A data.table containing a column named |
x, invisibly, with a new granularity_geo column containing the
ISO 3166-1 alpha-3 country code (always "nor" for Norwegian locations).
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.
Modify via set_config(); read directly (e.g. config$border_nor).
configconfig
Available variables:
border_nor (default 2024): the border year used when selecting
Norwegian geographic datasets. Valid values: 2024.
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 is derived from the
lowercase alphabetic prefix of the location code (e.g. "county_nor03" ->
"county"); the special prefix "norge" is mapped to "nation". When a
reference table is supplied, the granularity is looked up directly.
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 / data.frame
containing a column named |
location_reference |
A data.table with columns |
A character vector the same length as x (or with as many elements
as there are rows in x when x is a data.table), containing the
corresponding granularity_geo values.
csdata::location_code_to_granularity_geo(c("nation_nor", "county_nor03", "municip_nor0301")) library(data.table) dt <- data.table(location_code = c("nation_nor", "county_nor03")) csdata::location_code_to_granularity_geo(dt)csdata::location_code_to_granularity_geo(c("nation_nor", "county_nor03", "municip_nor0301")) library(data.table) dt <- data.table(location_code = c("nation_nor", "county_nor03")) csdata::location_code_to_granularity_geo(dt)
Returns the ISO 3166-1 alpha-3 country code for each location code.
Currently all Norwegian location codes map to "nor".
location_code_to_iso3(x)location_code_to_iso3(x)
x |
A character vector of location codes, or a data.table / data.frame
containing a column named |
A character vector the same length as x (or with as many elements
as there are rows in x when x is a data.table), containing the
corresponding ISO 3166-1 alpha-3 country code (always "nor").
csdata::location_code_to_iso3(c("nation_nor", "county_nor03", "municip_nor0301")) 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")) 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).
nbnb
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, in which case all requested combinations are
returned combined 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 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).
csdata::nor_locations_hierarchy_from_to(from = "wardoslo", to = "county") csdata::nor_locations_hierarchy_from_to(from = "municip", to = "baregion") csdata::nor_locations_hierarchy_from_to( from = c("municip", "county"), to = "georegion", include_to_name = TRUE )csdata::nor_locations_hierarchy_from_to(from = "wardoslo", to = "county") csdata::nor_locations_hierarchy_from_to(from = "municip", to = "baregion") csdata::nor_locations_hierarchy_from_to( from = c("municip", "county"), to = "georegion", include_to_name = TRUE )
Returns a data.table of all 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.
nor_locations_names(border = csdata::config$border_nor)nor_locations_names(border = csdata::config$border_nor)
border |
Integer. The geographic border year determining which
administrative boundaries are used. 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: 1-letter for nation and county, shorter display name for Oslo and Bergen 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".
https://no.wikipedia.org/wiki/Liste_over_norske_kommunenummer
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.
Location code as it existed in calyear.
The calendar year to which location_code_original
applies.
Proportional weight to apply when aggregating from the
original location to the current location (values sum to 1 within each
location_code_original / calyear group).
Geographic granularity: one of "nation",
"county", "municip", "wardbergen", "wardoslo",
"wardstavanger", "wardtrondheim", "missingwardbergen",
"missingwardoslo", "missingwardstavanger", "missingwardtrondheim",
"notmainlandcounty", "notmainlandmunicip", "missingcounty".
Statistics Norway (SSB) municipal reform documentation.
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, county, municipality, and city-district level.
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 specifying the age
values to include in each category. Each vector element defines one age
band. If a list element is named, that name is used as 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" in the current dataset.
Calendar year.
Population count as of 1 January of calyear.
Logical. TRUE if the value was imputed.
## Not run: # Not run: aggregates the full population dataset, which exceeds CRAN's # example time limit. # Default: return total population only d <- nor_population_by_age_cats() 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 ) d2[granularity_geo == "nation" & calyear == 2024] ## End(Not run)## Not run: # Not run: aggregates the full population dataset, which exceeds CRAN's # example time limit. # Default: return total population only d <- nor_population_by_age_cats() 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 ) d2[granularity_geo == "nation" & calyear == 2024] ## End(Not run)
A function that categorizes 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 containing vectors that you want to categorize. |
include_total_age |
Boolean. Should 'total' be included as an age cat? |
include_total_sex |
Boolean. Should 'total' (both sexes combined) be included as a sex, alongside 'male' and 'female'? |
include_9999 |
Boolean. Should the current year is duplicated and added as "calyear==9999". This is in accordance with the cstidy principles regarding granularity_time=="event_*". |
border |
The year in which Norwegian geographical boundaries were designated (2024). |
For locations where Statistics Norway provides no sex breakdown (Svalbard,
Jan Mayen, unknown), male and female are returned as NA while total
holds the real count.
Note that when include_total_sex = TRUE the output holds male, female and
total in long format, so summing pop_jan1_n across all sex values 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
## Not run: nor_population_by_sex_age_cats(cats = list(c(1:10), c(11:20))) ## End(Not run)## Not run: nor_population_by_sex_age_cats(cats = list(c(1:10), c(11:20))) ## End(Not run)
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).
sese
print(csdata::se) csdata::se$oe # lowercase oeprint(csdata::se) csdata::se$oe # lowercase oe
Updates one or more variables in the config environment. Call this at the start of a script to change the default border year used by all data-returning functions.
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: |
NULL, invisibly. Called for the side effect of updating config.
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