An R-package for accessing Statistikkbank data from Statistics Norway (SSB).
Statistics Norway (SSB) is Norway’s central statistical authority. It publishes hundreds of official statistics tables through its Statistikkbank service, covering topics such as population, economy, labour, housing, and health.
The StatistikkbankR package gives you a direct R interface to the Statistikkbank API. You can search the table catalogue, inspect table structure, and download data as tidy data.frame or tibble objects, directly in R.
This package has been written using GitHub Copilot with Codex and Claude.
Installation
Install the development version from GitHub:
# install.packages("remotes")
remotes::install_github("navikt/statistikkbankR")Nav firewall
If you work in Nav and use VDI Analyse, R may fail to verify SSL certificates and you get an error when trying to install. In that case, try running the command:
Sys.setenv(R_LIBCURL_SSL_REVOKE_BEST_EFFORT = "TRUE")Finding a table
Use ssb_search() to find tables by keyword. The function returns a data frame with one row per matching table; the id column is what you will pass to other functions.
library(StatistikkbankR)
# Search for tables about population (in Norwegian)
results <- ssb_search("befolkning")
results[, c("id", "label", "updated")]
# Or search in English
ssb_search("population", language = "en")SSB paginates search results; use fetch_all = TRUE if you want all pages at once.
# Retrieve all pages at once
all_results <- ssb_search("arbeid", fetch_all = TRUE)
nrow(all_results)Alternatively, you can find tables of interest on SSB’s website.
Inspecting a table before downloading
Once you have a table identifier, use ssb_describe() to see its structure before downloading it. This is the recommended first command to run once you have a table you’re interested in.
desc <- ssb_describe("07459")
# Table-level summary: title, last updated, estimated total rows
desc$table
# One row per dimension: id, label, role, number of codes
desc$dimensionsUse ssb_codes() to list the valid values you can use as filters for a given dimension:
Downloading data
Without filters
Pass a table identifier to get_ssb_data() to download the full table. SSB enforces a cell limit on large tables; the function will warn you and stop if the estimated row count is too high.
# Download all data (only works for small tables)
data <- get_ssb_data("05803")
head(data)Filtering by dimension
Pass dimension identifiers as named arguments to select only the rows you need. Each argument accepts a character vector of codes.
# Download population for specific regions for the years 2020, 2021 and 2022
pop <- get_ssb_data(
"07459",
Region = c("0301", "1103"),
Tid = 2020:2022
)
popServer-side expressions
SSB supports a set of shorthand expressions that are evaluated on the server before data is returned. You can use these as filter values:
| Expression | Meaning |
|---|---|
"top(n)" |
The n most recent time periods |
"bottom(n)" |
The n oldest time periods |
"all(*)" |
All codes (the default if you omit the argument) |
# The three most recent years for all regions
pop_recent <- get_ssb_data(
"07459",
Tid = "top(3)"
)For more details on the possible query expressions, see the SSB API documentation.
Long format and singleton dimensions
By default get_ssb_data() returns data in wide format: one column per measure code. Use table_format = "long" to get a long table instead, with one row per observation.
pop_long <- get_ssb_data(
"07459",
Region = c("0301", "1103"),
Tid = 2020:2022,
table_format = "long"
)
head(pop_long)When filtering to a single code in one dimension, that dimension collapses to a constant and is dropped from the wide output but retained in long output by default. Pass include_singleton_dims = TRUE with table_format = "long" to include those constant-value columns explicitly:
pop_one_region <- get_ssb_data(
"07459",
Region = "0301",
Tid = "top(3)",
table_format = "long",
include_singleton_dims = TRUE
)Time classes (zoo::yearqtr and zoo::yearmon)
By default, get_ssb_data() converts quarterly time codes to zoo::yearqtr and monthly time codes to zoo::yearmon.
# Quarterly table (12452): Tid becomes yearqtr by default
q_dat <- get_ssb_data(
"12452",
Tid = "top(4)",
character_as_factor = FALSE
)
class(q_dat$Tid_label)
# Disable quarter conversion and keep raw SSB codes like "2024K1"
q_raw <- get_ssb_data(
"12452",
Tid = "top(4)",
character_as_factor = FALSE,
convert_quarter_to_yearqtr = FALSE
)
head(q_raw$Tid_label)
# Monthly table (13966): Tid becomes yearmon by default
m_dat <- get_ssb_data(
"13966",
Tid = "top(4)",
character_as_factor = FALSE
)
class(m_dat$Tid_label)
# Disable month conversion and keep raw SSB codes like "2024M01"
m_raw <- get_ssb_data(
"13966",
Tid = "top(4)",
character_as_factor = FALSE,
convert_month_to_yearmon = FALSE
)
head(m_raw$Tid_label)Codelists and geographic boundary changes
SSB provides aggregation codelists that let you request data at a different grouping level — for example, historical municipality boundaries — without manually remapping individual codes.
SSB’s klassR package provides an easy interface for retrieving SSB classifications, and may be used alongside this package.
Use ssb_codelists() to discover which codelists are available:
ssb_codelists("07459")
# Filter to the geographic dimension
ssb_codelists("07459", dimension = "Region")Use ssb_codelist_details() to inspect the group-to-member mapping inside a specific codelist:
cl <- ssb_codelist_details("agg_KommSummer")
cl$codelist # metadata
cl$values # group codes and their membersApply a codelist when downloading data by passing the codelists and output_values arguments to get_ssb_data():
pop_agg <- get_ssb_data(
"07459",
Tid = 2020:2022,
codelists = list(Region = "agg_KommSummer"),
output_values = list(Region = "aggregated")
)Or use the convenience wrapper ssb_get_by_codelist() which identifies the correct dimension automatically:
pop_agg <- ssb_get_by_codelist(
"07459",
"agg_KommSummer",
Tid = 2020:2022,
output_value = "aggregated"
)To join the aggregated result back to individual municipality codes, use ssb_expand_codelist_mapping():
pop_expanded <- ssb_expand_codelist_mapping(
data = pop_agg,
code_col = "Region_code",
codelist_id = "agg_KommSummer"
)
head(pop_expanded)Performance tips
Caching
By default, get_ssb_data() and all helper functions cache table metadata in memory for the duration of your R session. Set cache = FALSE to disable caching, or refresh_metadata = TRUE to discard the cached copy and fetch fresh metadata:
# Force a fresh metadata fetch (e.g. after SSB updates the table)
get_ssb_data("07459", Tid = "top(1)", refresh_metadata = TRUE)For details on when SSB updates their tables, see their https://www.ssb.no/en/statbank/.
Large tables
SSB imposes a cell limit on queries. get_ssb_data() estimates the result size before sending the request and stops with a message if the query is too large. To proceed anyway, set override_large_query = TRUE:
data <- get_ssb_data(
"07459",
override_large_query = TRUE
)