Skip to content

Instantly share code, notes, and snippets.

@Straubinger
Last active November 13, 2021 17:16
Show Gist options
  • Select an option

  • Save Straubinger/3a800c2e421afb3db1b9b6109e452b02 to your computer and use it in GitHub Desktop.

Select an option

Save Straubinger/3a800c2e421afb3db1b9b6109e452b02 to your computer and use it in GitHub Desktop.
Create parent-child hierarchy of Danish municipalities and regions
# This code creates a parent-child hierarchy of Danish municipalities and regions based on classifications from Statistics Denmark
dl_csv <- read.csv2("https://www.dst.dk/klassifikationsbilag/5d18d1e0-400b-4505-92ad-6782915980a3csv_da",
encoding = "UTF-8")
classifications <- dl_csv %>%
select(KODE, NIVEAU, TITEL) %>%
group_by(gr = cumsum(NIVEAU == 1)) %>%
mutate(region = TITEL[NIVEAU == 1L]) %>%
ungroup() %>%
filter(NIVEAU == 3) %>%
select(-gr) %>%
left_join(dl_csv %>%
select(KODE, NIVEAU, TITEL) %>%
filter(NIVEAU != 1) %>%
group_by(gr = cumsum(NIVEAU == 2)) %>%
mutate(landsdel = TITEL[NIVEAU == 2L]) %>%
ungroup() %>%
filter(NIVEAU == 3) %>%
select(KODE, landsdel),
by = "KODE")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment