What about danstat?
Last updated on 2026-04-28 | Edit this page
Overview
Questions
- Is there an easier way to access Statistics Denmark?
Objectives
- Use a package to do the API-calls to Statistics Denmark
- Connect to Statistics Denmark, and extract data
- Create a list of lists to control the variables to be extracted
- Using the danstat package
Please note: These pages are autogenerated. Some of the API-calls may fail during that process. We are figuring out what to do about it, but please excuse us for any red errors on the pages for the time being.
Is there an easier way?
Many larger online services provide packages for easier access to their APIs.
Popular services might not have to do this, because enthusiasts write packages themselves.
A package called danstat is available, and makes it
easier to extract data from Statistics Denmark.
The danstat package/library
Previously we retrieved at table with demographic data from Statistics Denmark.
How can we get that table using the danstat package?
Before using the library, we will need to install it:
R
install.packages("danstat")
Some installations of R may have problems installing it. In that case, try this:
R
install.packages("remotes")
library(remotes)
remotes:install_github("cran/danstat")
After installation, we load the library using the library function. And then we can access the functions included in the library:
The danstat package contain four functions, equivalent to the four endpoints we discussed earlier.
The get_subjects() function sends a request to the
Statistics Denmark API, asking for a list of the subjects. The
information is returned to our script, and the
get_subjects() function presents us with a dataframe
containing the information.
R
library(danstat)
subjects <- get_subjects()
subjects
OUTPUT
id description active hasSubjects subjects
1 1 People TRUE TRUE NULL
2 2 Labour and income TRUE TRUE NULL
3 3 Economy TRUE TRUE NULL
4 4 Social conditions TRUE TRUE NULL
5 5 Education and research TRUE TRUE NULL
6 6 Business TRUE TRUE NULL
7 7 Transport TRUE TRUE NULL
8 8 Culture and leisure TRUE TRUE NULL
9 9 Environment and energy TRUE TRUE NULL
10 19 About Statistics Denmark TRUE TRUE NULL
We get the 10 major subjects from Statistics Denmark we have seen before. As before, each of them have sub-subjects.
If we want to take a closer look at the subdivisions of a given
subject, we use the get_subjects() function again, this
time specifying which subject we are interested in:
Let us try to get the sub-subjects from the subject 1 - containing information about populations and elections:
R
sub_subjects <- get_subjects(subjects = 1)
ERROR
Error in `curl::curl_fetch_memory()` at httr/R/write-function.R:78:3:
! Timeout was reached [api.statbank.dk]:
OpenSSL SSL_read: Connection reset by peer, errno 104
R
sub_subjects
ERROR
Error:
! object 'sub_subjects' not found
The result is a bit complicated. The column “subjects” in the resulting dataframe contains another dataframe. We access it like we normally would access a column in a dataframe:
R
sub_subjects$subjects
ERROR
Error:
! object 'sub_subjects' not found
We can continue diving into this, and will end up with subject “20021 Population figures”.
Which datatables exists?
We ended up with a specific subject,
20021 Population figures
And can use the get_tables() function to get information
about the tables available:
R
tables <- get_tables(subjects="20021")
ERROR
Error in `curl::curl_fetch_memory()` at httr/R/write-function.R:78:3:
! Timeout was reached [api.statbank.dk]:
Connection timeout after 10001 ms
R
tables |> head()
ERROR
Error:
! object 'tables' not found
We have seen this information before, and can now use the
get_table_metadata() function to extract metadata on
specific tables:
R
metadata <- get_table_metadata("FOLK1A", variables_only = TRUE)
ERROR
Error in `curl::curl_fetch_memory()` at httr/R/write-function.R:78:3:
! Failure when receiving data from the peer [api.statbank.dk]:
OpenSSL SSL_read: Connection reset by peer, errno 104
R
metadata
ERROR
Error:
! object 'metadata' not found
We use the variables_only = TRUE to remove eg. contact
information to Statistics Denmark.
What kind of values can the individual datapoints take?
R
metadata |>
slice(4) |>
pull(values)
ERROR
Error:
! object 'metadata' not found
We use the slice function from tidyverse to pull out the fourth row of the dataframe, and the pull-function to pull out the values in the values column.
The same trick can be done for the other fields in the table:
R
metadata |>
slice(1) |>
pull(values) |>
pluck(1) |>
head()
ERROR
Error:
! object 'metadata' not found
Here we see the individual municipalities in Denmark.
Which variables do we want?
As before we need to specify the variables we want in our answer.
These variables, and the values of them, need to be specified when we pull the data from Statistics Denmark.
We have seen how to do that using the POST() function,
it is done similarly using the danstat package:
R
variables <- list(list(code = "OMRÅDE", values = NA),
list(code = "CIVILSTAND", values = c("U", "G", "E", "F")),
list(code = "Tid", values = NA)
)
And now we can call the get_data() function and retrieve
data:
R
data <- get_data(table_id = "FOLK1A", variables = variables)
ERROR
Error in `curl::curl_fetch_memory()` at httr/R/write-function.R:78:3:
! Failure when receiving data from the peer [api.statbank.dk]:
OpenSSL SSL_read: Connection reset by peer, errno 104
It takes a short moment. But now we have a dataframe containing the data we requested:
R
head(data)
OUTPUT
1 function (..., list = character(), package = NULL, lib.loc = NULL,
2 verbose = getOption("verbose"), envir = .GlobalEnv, overwrite = TRUE)
3 {
4 fileExt <- function(x) {
5 db <- grepl("\\\\.[^.]+\\\\.(gz|bz2|xz)$", x)
6 ans <- sub(".*\\\\.", "", x)
- Larger services often provide packages to make it easier to use their API.