z22_data: Get Census 2022 grid dataset

View source: R/data.R

z22_dataR Documentation

Get Census 2022 grid dataset

Description

Retrieve the values and coordinates of gridded features from the censuses 2011 and 2022.

When we are talking about a

  • feature, we talk about an indicator aggregated to grid cells, e.g., age or the number of dwellings.

  • category, we talk about the discrete classifications of features, e.g., ages 10 to 19, 20 to 20, 30 to 39, etc.

  • Both feature and category have to be provided to uniquely identify a dataset.

Usage

z22_data(
  feature,
  categories = NULL,
  year = 2022,
  res = "1km",
  all_cells = FALSE,
  normalize = FALSE,
  as = c("df", "sf", "raster"),
  update_cache = FALSE
)

Arguments

feature

A grid feature. See z22_features for a list of available features. You can pass both English names and legacy names (i.e., variable names from the 2011 census).

categories

One or multiple feature categories. See z22_categories for a list of available categories. If NULL, retrieves all categories for a given feature. Generally, the more categories are selected, the longer the download.

year

Census year. Currently, only 2011 and 2022 are available. Defaults to 2022.

res

Resolution of the grid dataset. Can be "100m", "1km", or "10km". If year is 2011, "10km" is not available and some features are only available at certain resolutions.

all_cells

If TRUE, joins the retrieved attribute with the complete grid from z22_grid. Otherwise, the attribute grid will contain only those grid cells with one or more recorded units. Defaults to FALSE, because loading the grid and joining with it is computationally expensive. If the downloaded feature is a count or a share, all empty grid values will be replaced with 0.

normalize

If TRUE and feature is a counted feature, computes shares by dividing the counts by the total number of units in the grid cell. Note that this operation downloads all remaining categories of a feature, which can take some time for low resolutions. The total is based on the sum of subtotals so that shares are capped at 100 details. Defaults to FALSE.

as

Specifies the output class. Must be one of "df", "sf", or "raster". If "df" (default), returns flat coordinates in a dataframe. If "sf" (and the sf package is installed), converts the coordinates to an sf tibble. If "raster" (and the terra package is installed), converts the coordinates to a SpatRaster.

update_cache

By default, both functions cache attribute files for the remainder of the R session. They are downloaded to a temporary directory and - if the file to download already exists - are recovered from the cache. In other words, when rerunning the same request multiple times, the subsequent calls should be much faster. If TRUE, disables caching for this call and overwrites the currently cached attribute file (if any) with a fresh one. Defaults to FALSE, i.e. always cache.

Details

By default, data are downloaded from the z22data data repository which stores all pre-processed data. You can download this repository and use it offline or use an entirely different repository by setting options(z22.data_repo = "path/to/z22data").

Census data are protected using the cell-key method. As a consequence, cell values are confidentialized by perturbation and often do not represent true values. The mean absolute deviation for counts is below 1. 65 % of counts have a maximum perturbation of 1, 20 % of 3 or more and 10 % of 4 or more (Enderle and Kleber 2024). Population totals (such as "population", or "dwellings") add perturbation after adding up subtotals to preserve accuracy. Hence, the sum of subtotals can deviate from the totals, e.g., the sum of each category in "household_size_group" and the value of "households" can be different. The normalize argument takes this into account and computes shares based on the sum of subtotals instead as suggested by Enderle and Kleber (2024).

Value

A tibble, SpatRaster or sf tibble depending on the as argument.

If a tibble is returned each category in categories is stored in a column. If a SpatRaster is returned, each category is a named layer.

References

Enderle, T., Kleber, B., 2024. Geheimhaltung mit der Cell-Key-Methode im Zensus 2022. WISTA Wirtschaft und Statistik 6/2024, 83-91.

Examples


# Get gridded population
pop <- z22_data("population", res = "10km", as = "raster")
terra::plot(pop$cat_0)

# Get data about the number of people born in a EU27 country
z22_data("birth_country", categories = 21, res = "1km")


z22 documentation built on Nov. 10, 2026, 5:07 p.m.