Skip to content

Instantly share code, notes, and snippets.

@patperu
Created September 29, 2019 17:40
Show Gist options
  • Save patperu/6caf72abbd76f24e8d15896acec97ecf to your computer and use it in GitHub Desktop.
Save patperu/6caf72abbd76f24e8d15896acec97ecf to your computer and use it in GitHub Desktop.
Auspendler Schleswig-Hostein, BA-Daten
``` r
library(readxl)
library(tidyverse)
library(purrr)
read_pendler <- function(x, sheet) {
readxl::read_xlsx(x, sheet = sheet, skip = 8, col_names = FALSE, col_types = "text") %>%
mutate(Jahr = str_extract(x, "(\\d{4})"),
Jahr = as.Date(paste(Jahr, '06-30', sep = '-')))
}
(pendler_files <- dir('data/sh', pattern = '.xlsx', full.names = TRUE))
#> [1] "data/sh/krpend_01_0-2013.xlsx" "data/sh/krpend_01_0-2014.xlsx"
#> [3] "data/sh/krpend_01_0-2015.xlsx" "data/sh/krpend_01_0-2016.xlsx"
#> [5] "data/sh/krpend_01_0-2017.xlsx" "data/sh/krpend_01_0-2018.xlsx"
auspendler <- purrr::map_df(pendler_files, read_pendler, 'Auspendler Kreise') %>%
set_names(c('gem_wo', 'Wohnort', 'gem_ao', 'Arbeitsort',
'Insgesamt', 'Männer', 'Frauen', 'Deutsche',
'Ausländer_Sonstige', 'Auszubildende', 'Jahr')) %>%
fill(gem_wo:Wohnort) %>%
filter(!is.na(gem_ao)) %>%
mutate(Pendlergruppe = 'Auspendler')
#> New names:
#> * `` -> ...1
#> * `` -> ...2
#> * `` -> ...3
#> * `` -> ...4
#> * `` -> ...5
#> * … and 5 more problems
#> New names:
#> * `` -> ...1
#> * `` -> ...2
#> * `` -> ...3
#> * `` -> ...4
#> * `` -> ...5
#> * … and 5 more problems
#> New names:
#> * `` -> ...1
#> * `` -> ...2
#> * `` -> ...3
#> * `` -> ...4
#> * `` -> ...5
#> * … and 5 more problems
#> New names:
#> * `` -> ...1
#> * `` -> ...2
#> * `` -> ...3
#> * `` -> ...4
#> * `` -> ...5
#> * … and 5 more problems
#> New names:
#> * `` -> ...1
#> * `` -> ...2
#> * `` -> ...3
#> * `` -> ...4
#> * `` -> ...5
#> * … and 5 more problems
#> New names:
#> * `` -> ...1
#> * `` -> ...2
#> * `` -> ...3
#> * `` -> ...4
#> * `` -> ...5
#> * … and 5 more problems
glimpse(auspendler)
#> Observations: 14,063
#> Variables: 12
#> $ gem_wo <chr> "01001", "01001", "01001", "01001", "01001", …
#> $ Wohnort <chr> "Flensburg, Stadt", "Flensburg, Stadt", "Flen…
#> $ gem_ao <chr> "01002", "01003", "01004", "01051", "01054", …
#> $ Arbeitsort <chr> "Kiel, Landeshauptstadt", "Lübeck, Hansestadt…
#> $ Insgesamt <chr> "487", "146", "109", "37", "675", "20", "21",…
#> $ Männer <chr> "290", "107", "89", "31", "399", "7", "17", "…
#> $ Frauen <chr> "197", "39", "20", "6", "276", "13", "4", "10…
#> $ Deutsche <chr> "477", "129", "*", "*", "648", "*", "18", "*"…
#> $ Ausländer_Sonstige <chr> "10", "17", "*", "*", "27", "*", "3", "*", "1…
#> $ Auszubildende <chr> "12", "4", "*", "*", "33", "*", "*", "0", "14…
#> $ Jahr <date> 2013-06-30, 2013-06-30, 2013-06-30, 2013-06-…
#> $ Pendlergruppe <chr> "Auspendler", "Auspendler", "Auspendler", "Au…
write_csv2(auspendler, path = 'auspendler-sh.csv')
```
<sup>Created on 2019-09-29 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0.9000)</sup>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment