combine duplicate redner by collapsing all data

This commit is contained in:
2021-06-24 19:57:26 +02:00
parent 72a32c5cc7
commit a7e9ba9655
2 changed files with 19 additions and 4 deletions
+17 -4
View File
@@ -8,16 +8,28 @@ fraktionen <- c("AFD" = "AfD",
"CDU/CSU" = "CDU/CSU", "CDU/CSU" = "CDU/CSU",
"FDP" = "FDP") "FDP" = "FDP")
# expects a tibble of redner and repairs
repair_fraktion <- function(fraktion) { repair_fraktion <- function(fraktion) {
cleaned <- str_to_upper %$% str_replace_all(fraktion, "\\s", "") cleaned <- str_to_upper %$% str_replace_all(fraktion, "\\s", "")
fraktionen[cleaned] fraktionen[cleaned]
} }
# takes vector of titel and keeps longest
longest_titel <- function(titel) {
if (all(is.na(titel))) NA_character_
else titel[which.max %$% str_length(titel)]
}
# takes character vector, removes duplicates and collapses
collect_unique <- function(xs) xs %>% clear_na() %>% unique() %>% str_c(collapse="&") %>% na_if("")
# expects a tibble of redner and repairs
repair_redner <- function(redner) { repair_redner <- function(redner) {
# fix fraktionsnames redner %>% mutate(fraktion = Vectorize(repair_fraktion)(fraktion)) %>% # fix fraktion
redner %>% mutate(fraktion = Vectorize(repair_fraktion)(fraktion)) group_by(id, vorname, nachname) %>%
summarize(fraktion = collect_unique(fraktion),
titel = longest_titel(titel),
rolle_kurz = collect_unique(str_squish(rolle_kurz)),
rolle_lang = collect_unique(str_squish(rolle_lang)))
} }
repair_reden <- function(reden) { repair_reden <- function(reden) {
@@ -36,3 +48,4 @@ repair <- function(parse_output) {
reden = repair_reden(parse_output$reden), reden = repair_reden(parse_output$reden),
talks = repair_talks(parse_output$talks)) talks = repair_talks(parse_output$talks))
} }
+2
View File
@@ -1,2 +1,4 @@
`%$%` <- function(f, x) f(x) `%$%` <- function(f, x) f(x)
`%.%` <- function(f, g) function(...) f(g(...)) `%.%` <- function(f, g) function(...) f(g(...))
clear_na <- function(xs) xs[!is.na(xs)]