This repository has been archived by the owner on Apr 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Social_DargeboteneHand.Rmd
222 lines (188 loc) · 9.29 KB
/
Social_DargeboteneHand.Rmd
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
---
title: "Social -- Dargebotene Hand"
output:
html_document:
toc: true
toc_float: true
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
# knitr options
knitr::opts_chunk$set(echo = FALSE)
# load packages
library(dplyr)
library(tidyr)
library(readr)
library(stringr)
library(lubridate)
library(purrr)
library(janitor)
library(here)
library(readxl)
```
# Import excel data
```{r 'read data'}
dh_path <- Sys.getenv("dh_path")
file_paths <- here(dh_path, list.files(path = dh_path))
dat_in <- map(file_paths, ~read_excel(path = .x, skip = 2))
```
# Data tidying
Prepare clean variables and rename unnamed variables.
```{r 'clean data'}
clean_data <- function(raw_data){
data_cleaned <- raw_data %>%
clean_names() %>%
rename(gruppe = x1,
variable = x2) %>%
# remove rows with NAs only
filter_all(any_vars(!is.na(.))) %>%
slice(-1) %>%
# complete dataframe
fill(gruppe, .direction = "down") %>%
# remove some variables
filter(!(grepl(pattern = c("Arit|Median|Elt"), x = variable))) %>%
filter(!(grepl(pattern = c("hnreg|erhil|Sprache"), x = gruppe))) %>%
# remove percentage data
select(-in_prozent)
return(data_cleaned)
}
dat_clean <- map(dat_in, ~clean_data(raw_data = .x))
```
## Validation 1: check sums
Compare the total number of calls in column `Anzahl` to the sum of calls in variable `Gruppe` for value 'Schicht'.
```{r 'check sums'}
check1 <- function(raw_data, cleaned_data){
anzahl_anrufe_total <- raw_data %>%
slice(1) %>%
.$Anzahl
check <- cleaned_data %>%
filter(gruppe == "Schicht") %>%
group_by(gruppe) %>%
summarise(
sum_anzahl = sum(anzahl)
) %>%
ungroup() %>%
mutate(
check_boolean = anzahl_anrufe_total == sum_anzahl
)
# Stop script if check is FALSE
if (check$check_boolean == FALSE) {
stop("Check 1 of Validation 1 is FALSE. Please evaluate error in code chunk 'check-1'")
} else {
return(TRUE)
}
}
# The check function is applied to each file
map2_lgl(.x = dat_in, .y = dat_clean, ~check1(raw_data = .x, cleaned_data = .y)) %>%
all()
```
# Add metadata
Relevant metadata is stored in the file names. This metadata is extracted and added to the cleaned data.
```{r 'extract metadata'}
add_metadata <- function(cleaned_data, path_to_files){
# number of files
no_files <- length(list.files(path = path_to_files))
# extract file_id, month and year from file names
dat_filename <- tibble(
file_id = seq(no_files),
file_name = list.files(path = path_to_files)) %>%
separate(col = file_name, into = c("a", "b", "c", "d"), sep = c(19, 24, 26)) %>%
separate(col = d, into = c("e", "f"), sep = ".xlsx") %>%
select(-a, -e, -f) %>%
mutate(jahr = str_replace(b, pattern = "-", replacement = "")) %>%
select(-b) %>%
rename("monat" = c) %>%
mutate(jahr = str_replace(jahr, "_", "")) %>%
split(seq(no_files))
# combine cleaned data and extracted metadata
dat_combined <- map2(.x = dat_filename, .y = cleaned_data, ~cbind(.x, .y))
return(dat_combined)
}
dat_final <- add_metadata(dat_clean, dh_path)
```
# Bring monthly data into Covid19-Monitoring format
```{r 'covid19 monitoring format'}
dat_export <- bind_rows(dat_final) %>%
rename("value" = anzahl) %>%
mutate(monat = as.numeric(monat)) %>%
unite(date, c("monat", "jahr"), sep = "-", remove = F) %>%
mutate(date = dmy(paste(days_in_month(monat), format(date, format = "%m-%y"), sep = "-"))) %>%
select(-file_id, -monat, -jahr) %>%
group_by(date, gruppe) %>%
# summarize Schicht group
summarize(variable = if_else(gruppe == "Schicht", "total", variable),
value = if_else(gruppe == "Schicht", sum(value), value),
date = date) %>%
ungroup() %>%
distinct() %>%
mutate(gruppe = if_else(gruppe == "Schicht", "Kontaktaufnahmen", gruppe),
variable = case_when(variable == "Ablehnung, ev. gesperrt" ~ "Schweigeanruf",
variable == "Vereinbarung" ~ "Regelmässiger Kontakt",
TRUE ~ variable)) %>%
unite(variable_long, c("gruppe", "variable"), sep = " ") %>%
group_by(date, variable_long) %>%
summarize(value = sum(value)) %>%
ungroup() %>%
complete(date, nesting(variable_long)) %>%
mutate(topic = "Soziales",
location = "ZH",
unit = "Anzahl",
source = "Dargebotene Hand",
update = "monatlich",
public = "ja",
description = "https://github.com/statistikZH/covid19monitoring_social_DargeboteneHand/blob/main/README.md") %>%
relocate(c(date,value, topic), .before = variable_long) %>%
mutate(variable_short = case_when(variable_long == "Kontaktaufnahmen total" ~ "kontaktaufnahmen_total",
variable_long == "Geschlecht Weiblich" ~ "geschlecht_weiblich",
variable_long == "Geschlecht Männlich" ~ "geschlecht_maennlich",
variable_long == "Geschlecht nicht bestimmbar" ~ "geschlecht_unbestimmt",
variable_long == "Alter bis 18 J." ~ "alter_bis_18",
variable_long == "Alter 19 - 40 J." ~ "alter_19_bis_40",
variable_long == "Alter 41 - 65 J." ~ "alter_41_bis_65",
variable_long == "Alter über 65 J." ~ "alter_ue65",
variable_long == "Alter nicht bestimmbar" ~ "alter_unbestimmt",
variable_long == "Kontakthäufigkeit Erster Kontakt" ~ "kontakt_erstmalig",
variable_long == "Kontakthäufigkeit Gelegentlicher, wiederholter Kontakt" ~ "kontakt_gelegentlich",
variable_long == "Kontakthäufigkeit Regelmässiger Kontakt" ~ "kontakt_regelmaessig",
variable_long == "Beratungsinhalt Alltagsbewältigung" ~ "inhalt_alltagsbewaeltigung",
variable_long == "Beratungsinhalt Psychisches Leiden" ~ "inhalt_leiden_psychisch",
variable_long == "Beratungsinhalt Sorge wegen Infektion" ~ "inhalt_sorge_infektion",
variable_long == "Beratungsinhalt Körperliches Leiden" ~ "inhalt_leiden_koerperlich",
variable_long == "Beratungsinhalt Einsamkeit" ~ "inhalt_einsamkeit",
variable_long == "Beratungsinhalt Familie / Erziehung" ~ "inhalt_familie_erziehung",
variable_long == "Beratungsinhalt Beziehung allgemein" ~ "inhalt_beziehung_allgemein",
variable_long == "Beratungsinhalt Paarbeziehung" ~ "inhalt_paarbeziehung",
variable_long == "Beratungsinhalt Arbeit / Ausbildung" ~ "inhalt_arbeit_ausbildung",
variable_long == "Beratungsinhalt Existenzprobleme" ~ "inhalt_existenzprobleme",
variable_long == "Beratungsinhalt Gewalt" ~ "inhalt_gewalt",
variable_long == "Beratungsinhalt Sexualität" ~ "inhalt_sexualitaet",
variable_long == "Beratungsinhalt Spiritualität / Lebenssinn" ~ "inhalt_spiritualitaet_lebenssinn",
variable_long == "Beratungsinhalt Suchtverhalten" ~ "inhalt_suchtverhalten",
variable_long == "Beratungsinhalt Suizidalität" ~ "inhalt_suizidalitaet",
variable_long == "Beratungsinhalt Verlust / Trauer / Tod" ~ "inhalt_verlust_trauer_tod",
variable_long == "Beratungsinhalt Verschiedenes" ~ "inhalt_verschiedenes",
variable_long == "Beratungsinhalt Medienerziehung/Swisscom" ~ "inhalt_medienerziehung_swisscom",
variable_long == "Zusätzliche Beanspruchung Verschobenes Gespräch" ~ "zus_beanspruchung_verschobenes_gespraech",
variable_long == "Zusätzliche Beanspruchung Schweigeanruf" ~ "zus_beanspruchung_schweigeanruf",
variable_long == "Zusätzliche Beanspruchung Fehlanruf" ~ "zus_beanspruchung_fehlanruf",
variable_long == "Zusätzliche Beanspruchung Juxanruf" ~ "zus_beanspruchung_juxanruf",
variable_long == "Zusätzliche Beanspruchung aufgelegt" ~ "zus_beanspruchung_aufgelegt",
variable_long == "Geschlecht Divers" ~ "geschlecht_divers",
variable_long == "Zusätzliche Beanspruchung Chat ohne Inhalt" ~ "zus_beanspruchung_chat_ohne_inhalt",
TRUE ~ "no_case"
)) %>%
relocate(variable_short, .after = variable_long)
```
## Validation 2: check variable_short
```{r 'checkk variable_short'}
# check whether there are any "no_case" variables
if(any(dat_export$variable_short == "no_case")){
stop("Some variables do not have a 'variable_short' definition!")
}
```
# Data Export
Write CSV for monthly data.
```{r 'write data'}
write_csv(dat_export, here("Social_DargeboteneHand.csv"))
```