-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataprep.R
123 lines (103 loc) · 3.31 KB
/
dataprep.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
library(readr)
library(dplyr)
library(purrr)
library(stringr)
library(tidyr)
library(broom)
# library(fs)
library(rgdal)
#### read data ####
# skip first 4 lines, set encoding, define delimiter
# Erststimmen
erst <- read_delim("data/btw17_wbz_erststimmen.csv",
";", escape_double = FALSE, locale = locale(encoding = "CP1252"),
trim_ws = TRUE, skip = 4) %>%
mutate(
Bundesland = as.integer(Land)
)
# Zweitstimmen
zweit <- read_delim("data/btw17_wbz_zweitstimmen.csv",
";", escape_double = FALSE, locale = locale(encoding = "CP1252"),
trim_ws = TRUE, skip = 4) %>%
mutate(
Bundesland = as.integer(Land)
)
# same with data containing all labels
leitband <- read_delim("data/btw17_wbz_leitband.csv",
";", escape_double = FALSE, locale = locale(encoding = "CP1252"),
trim_ws = TRUE, skip = 4)
laender <- leitband[1:16, ]
# build a function to allocate Federal State Name to number
num2name <- function(x = nummer){
laender$Name[laender$Land == x]
}
erst <- erst %>%
mutate(
# map_chr() runs num2name on every entry of btw17$Land
Bundesland = map_chr(Bundesland, num2name)
)
zweit <- zweit %>%
mutate(
# map_chr() runs num2name on every entry of btw17$Land
Bundesland = map_chr(Bundesland, num2name)
)
#### aggregate data ####
erst_long <- erst %>%
select(Bundesland, `Wahlberechtigte (A)`, `Wähler (B)`, `Ungültige`:`Übrige`) %>%