knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(statficlassifications)

For regional classifications, statficlassifications has tailored tools.

Getting regional classifications

To load regional classifications, use get_regionclassification:

region_classification <- get_regionclassification()
head(region_classification)
seutukunta_classification <- get_regionclassification("seutukunta")
head(seutukunta_classification)
kunta_2010_classification <- get_regionclassification("kunta", year = 2010)
head(kunta_2010_classification)

Getting regional classification keys

To load regional correspondence tables / classification keys, use get_regionkey:

regionkey <- get_regionkey(only_names = TRUE)
head(regionkey)
regionkey <- get_regionkey(only_codes = TRUE)
head(regionkey)

You can also get more specialised regional classification keys by setting arguments:

kunta_maakunta_key <- get_regionkey("kunta", "maakunta")
head(kunta_maakunta_key)

Manipulate regional variables

statficlassifications also gives you a selection of ways and functions that help you to standardize and manipulate the regional information in your data. For examples, generate random municipal data:

data <- get_regionkey() |> dplyr::select(kunta_name) |> dplyr::mutate(values = rnorm(dplyr::n()))
head(data)

You can use regional classification tables to add regions to your data:

dplyr::left_join(data, get_regionkey(only_names = TRUE), by = "kunta_name") |> head()

For a shortcut, use add_region:

 data |> add_region("maakunta") |> head()

It is also straightforward to compute, say, maakunta-level means give the municipal data.

data |> add_region("maakunta") |> 
        dplyr::group_by(maakunta) |>
       dplyr::summarize(maakunta_mean = mean(values)) |> head()

Region code prefixes

statficlassifications aims to impose the use of prefixed region codes. Prefixes are useful in making sure codes do not map to multiple names. There are, for instance, kuntia and seutukuntia with identical numbers. Prefixes help distinguish between these. The correpondence of region names and prefixes is

statficlassifications:::prefix_name_key

Function set_region_codes helps you set the prefixes of you region codes:

v <- c("191", "047", "063")
set_region_codes(v)

If your input vector has codes that may match to multiple region codes, you get an error without giving more information. If you know the region level to which your codes correspond to you can submit the region level to region_level-argument.

v <- c("5", "47", "20")
set_region_codes(v, region_level = "kunta")

Often you find data where kunta codes are not prefixed but the codes of other regions are. With your information on to which region level codes the codes without prefixes should be mapped, you can restrict the domain of this mapping by providing the region_level-argument:

v <- c("020", "047", "005", "MK01", "MK02")
set_region_codes(v, region_level = "kunta")

Again sometimes you have codes of multiple region levels mixed in the data without prefixes but the charachter length of the codes might make a difference. In these cases you can set use_char_length_info = TRUE. The default is way to use character length information is that three characters incidate kuntia and two characters indicate maakuntia.

v <- c("020", "047", "005", "01", "02")
set_region_codes(v, use_char_length_info = TRUE)

Prefixed region codes map unambiguosly to region names so it is then easy to map the codes to names. Note, however, that some region names do not map uniquely to region codes. In these cases you have to supply the region_level-argument again.

v <- c("020", "047", "005", "MK01", "MK02")
v <- set_region_codes(v, region_level = "kunta")
codes_to_names(v)
v <- codes_to_names(v)
names_to_codes(v)


pttry/statficlassifications documentation built on Jan. 17, 2024, 4:36 p.m.