Skip to contents

csdata provides structural reference data for Norway: location hierarchies, population counts, and the conventions used across the csverse format.

See https://niphr.github.io/csdata/reference/index.html for an overview of all available datasets and functions.

What csdata is for

csdata holds the reference tables that Norwegian surveillance work needs again and again:

  • which places exist, and how they nest inside each other;
  • how place boundaries were redistricted from 2006 to 2024;
  • how many people live in each place, by age and sex.

Every table ships inside the package, so no function call downloads data. Each analysis then stops carrying its own copy of the same lookups.

csdata exports thirteen objects. These five do most of the work.

Function Use it when you need
nor_locations_names() The list of places, and which level each place sits at.
nor_locations_hierarchy_from_to(from, to) To map one level onto another, such as every municipality to its county.
nor_locations_redistricting() Weights to move data recorded under older borders onto the 2024 borders.
nor_population_by_age_cats(cats) Denominators: population in the age bands you choose.
nor_population_by_sex_age_cats(cats) The same denominators, split by sex.

All five take a border argument. 2024 is the only value they accept. Any other value stops with an error.

Two current limitations

BA-regions are missing from the hierarchy table. nor_locations_names() lists 159 BA-regions, and nor_locations_hierarchy_from_to() accepts "baregion". But the bundled hierarchy table carries no BA-region codes at all. Every from/to pair that names "baregion" therefore returns an empty table instead of an error.

nrow(csdata::nor_locations_hierarchy_from_to(from = "municip", to = "baregion"))
#> [1] 0
nrow(csdata::nor_locations_names()[granularity_geo == "baregion"])
#> [1] 159

One location code is used twice. Two different laboratories share the code lab_nor084467.

loc <- csdata::nor_locations_names()
loc[location_code == "lab_nor084467", .(location_code, location_name)]
#>    location_code              location_name
#>           <char>                     <char>
#> 1: lab_nor084467      VV-HF Drammen Sykehus
#> 2: lab_nor084467 Laboratoriet Bærum sykehus

This matters when you pass nor_locations_names() as location_reference, because the lookup is a join on location_code. The duplicated code matches twice, so you get more values back than you put in.

csdata::location_code_to_granularity_geo(
  c("nation_nor", "county_nor03", "lab_nor084467"),
  location_reference = loc
)
#> [1] "nation" "county" "lab"    "lab"

add_granularity_geo_to_data_set() then fails, because it tries to write those four values into a three-row table.

x <- data.table(location_code = c("nation_nor", "county_nor03", "lab_nor084467"))
csdata::add_granularity_geo_to_data_set(x, location_reference = loc)
#> Error in `[.data.table`:
#> ! Supplied 4 items to be assigned to 3 items of column 'granularity_geo'. If you wish to 'recycle' the RHS please use rep() to make this intent clear to readers of your code.

Leave location_reference unset to avoid both problems. The granularity then comes from the code prefix, one value per input.

csdata depends on no other cs package, so you can install it on its own. cstidy imports it. csmaps uses the same location_code values for the 2024 borders, so its map polygons join straight onto csdata tables.

Two vignettes go deeper:

The rest of this page gives the csverse coding rules for locations, ages and sex.

Location

csdata::nor_locations_names() lists the valid locations and location types. The table below strikes through entries with uncommon or internal use.

Valid locations and location types in the csverse format
Geo (Granularity) N
Examples
location_code1 location_name2 location_name_description_nb3 location_name_file_nb_utf4 location_name_file_nb_ascii5
nation 1 nation_nor Norge-Noreg-Norway Norge-Noreg-Norway Norge_Noreg_Norway norge_noreg_norway
georegion6 5 georegion_nor1 Nord-Norge-Davvi-Norggas Nord-Norge-Davvi-Norggas (landsdel) Nord_Norge_Davvi_Norggas_landsdel nord_norge_davvi_norggas_landsdel
county 15 county_nor42 Agder Agder (fylke) Agder_fylke agder_fylke
notmainlandcounty 2 notmainlandcounty_nor22 Utenfor fastlands-Norge (Jan Mayen) Utenfor fastlands-Norge (Jan Mayen) (fylke) Utenfor_fastlands_Norge_Jan_Mayen_fylke utenfor_fastlands_norge_jan_mayen_fylke
missingcounty 1 missingcounty_nor99 Ukjent fylke Ukjent fylke (fylke) Ukjent_fylke ukjent_fylke
municip 357 municip_nor1820 Alstahaug Alstahaug (kommune i Nordland-Nordlándda) Alstahaug_kommune_i_Nordland_Nordlándda alstahaug_kommune_i_nordland_nordlandda
notmainlandmunicip 2 notmainlandmunicip_nor2200 Jan Mayen Jan Mayen (kommune i Utenfor fastlands-Norge (Jan Mayen)) Jan_Mayen_kommune_i_Utenfor_fastlands_Norge_Jan_Mayen jan_mayen_kommune_i_utenfor_fastlands_norge_jan_mayen
missingmunicip 1 missingmunicip_nor9999 Ukjent kommune Ukjent kommune (kommune i Ukjent fylke) Ukjent_kommune_i_Ukjent_fylke ukjent_kommune_i_ukjent_fylke
wardoslo 15 wardoslo_nor030112 Alna Alna (bydel i Oslo-Oslove) Alna_bydel_i_Oslo_Oslove alna_bydel_i_oslo_oslove
wardbergen 8 wardbergen_nor460101 Arna Arna (bydel i Bergen) Arna_bydel_i_Bergen arna_bydel_i_bergen
wardstavanger 9 wardstavanger_nor110303 Eiganes og Våland Eiganes og Våland (bydel i Stavanger) Eiganes_og_Våland_bydel_i_Stavanger eiganes_og_valand_bydel_i_stavanger
wardtrondheim 4 wardtrondheim_nor500104 Heimdal Heimdal (bydel i Trondheim) Heimdal_bydel_i_Trondheim heimdal_bydel_i_trondheim
extrawardoslo 2 extrawardoslo_nor030117 Marka Marka (bydel i Oslo-Oslove) Marka_bydel_i_Oslo_Oslove marka_bydel_i_oslo_oslove
missingwardbergen 1 missingwardbergen_nor460199 Ukjent bydel i Bergen Ukjent bydel i Bergen (bydel i Bergen) Ukjent_bydel_i_Bergen ukjent_bydel_i_bergen
missingwardoslo 1 missingwardoslo_nor030199 Ukjent bydel i Oslo Ukjent bydel i Oslo (bydel i Oslo-Oslove) Ukjent_bydel_i_Oslo_Oslove ukjent_bydel_i_oslo_oslove
missingwardstavanger 1 missingwardstavanger_nor110399 Ukjent bydel i Stavanger Ukjent bydel i Stavanger (bydel i Stavanger) Ukjent_bydel_i_Stavanger ukjent_bydel_i_stavanger
missingwardtrondheim 1 missingwardtrondheim_nor500199 Ukjent bydel i Trondheim Ukjent bydel i Trondheim (bydel i Trondheim) Ukjent_bydel_i_Trondheim ukjent_bydel_i_trondheim
baregion7 159 baregion_nor111 Alstahaug Alstahaug (BA-region) Alstahaug_BA_region alstahaug_ba_region
mtregion8 5 mtregion_nor4 Midt-Norge Midt-Norge (Mattilsynet-region) Midt_Norge_Mattilsynet_region midt_norge_mattilsynet_region
lab 26 lab_nor000030 Akershus Universitetssykehus Akershus Universitetssykehus (lab) Akershus_Universitetssykehus_lab akershus_universitetssykehus_lab
1 location_code: Used a) inside datasets and b) in data file names for transfer of data/results between analytic systems. All values are unique.
2 location_name: Used (rarely) inside results (figures, tables, documents). Can be confusing as some names are duplicated. Its rare usage is demarcated by a line through the text.
3 location_name_description_nb: Used (frequently) inside results (figures, tables, documents). All values are unique.
4 location_name_file_nb_utf: Used (frequently) in the file names for results (figures, tables, documents). All values are unique.
5 location_name_file_nb_ascii: Used (rarely) in the file names for results (figures, tables, documents). Used if file systems have problems with the Norwegian letters æøå. All values are unique.
6 Landsdeler/riskdeler. Geographical regions.
7 Bo- og arbeidsmarkedsregioner. Housing and labor market regions.
8 Mattilsynet-regioner. Food authority regions.

Ages

An age MUST be coded as a character. An age MUST contain three digits. For an age range, join the two ages with an underscore (e.g. 005_010).

Use 085p rather than >=085 or 85+. This keeps the conversion from long to wide format straightforward.

Valid ages in the csverse format
Value class Definition
"000" character One year age group (0 year olds)
"079" character One year age group(79 year olds)
"000_004" character Age span of 0-4 year olds
"065p" character Age span of >=65 year olds
"missing" character Missing/unknown
"total" character Everyone

This format keeps data sorted correctly. It also produces valid variable names in wide format.

A missing age SHOULD be coded as "missing".

Sex

Sex MUST be coded as a character.

Valid sexes in the csverse format
Value class Definition
"male" character Male
"female" character Female
"missing" character Missing/unknown
"total" character Everyone

A missing sex SHOULD be coded as "missing".