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
|
get_initial_landkreis_data <- function(force_refresh = FALSE) {
if (file.exists(cache_filename) & force_refresh == FALSE) {
load(cache_filename)
# refetch <- (data_landkreise_detail_converted %>% pull(MeldedatumKlar) %>% max()) < Sys.Date() - days(1)
force_refresh <- FALSE
} else{
force_refresh <- TRUE
}
# Refresh only every hour
if (force_refresh && file.exists(cache_filename)) {
if(difftime(Sys.time() , file.info(cache_filename)$ctime , units = "mins") < 60) {
force_refresh <- FALSE
load(cache_filename)
}
}
if(force_refresh) {
Sys.setlocale(category = "LC_ALL", locale = "de_DE.UTF-8")
# url_rki <- "https://opendata.arcgis.com/datasets/dd4580c810204019a7b8eb3e0b329dd6_0.csv"
url_rki <- "https://www.arcgis.com/sharing/rest/content/items/f10774f1c63e40168479a1feb6c7ca74/data"
data_landkreise_detail_converted <- read_csv(url_rki) %>%
mutate(
Meldedatum = as.Date(
strptime(Meldedatum, format = "%Y/%m/%d %H:%M:%S")
),
Datenstand = as.Date(
strptime(Datenstand, format = "%d.%m.%Y, %H:%M Uhr")
)
)
save(data_landkreise_detail_converted, file = cache_filename)
}
data_landkreise_per_day <- data_landkreise_detail_converted %>%
filter(NeuerFall %in% c(1, 0)) %>%
complete(Meldedatum = seq(min(Meldedatum), max(Meldedatum), by = "day"),
IdLandkreis, fill = list(AnzahlFall = 0, NeuerFall = 1)) %>%
group_by(IdLandkreis, Meldedatum) %>%
summarize(
infected = sum(AnzahlFall)
) %>%
ungroup() %>%
complete(Meldedatum, IdLandkreis, fill = list(infected = 0)) %>%
arrange(Meldedatum) %>%
group_by(IdLandkreis) %>%
mutate(
infected_7 = rollsum(infected, 7, fill = NA, align = "right"),
infected_7_before = rollsum(lag(infected, n = 7), 7, fill = NA, align = "right"),
delta_7 = infected_7 - infected_7_before,
gesamt = cumsum(infected)
) %>%
left_join(landkreise) %>%
mutate(
infected_7_per_100k = round(infected_7 / Bevoelkerung * 100000, 1),
delta_7_per_100k = round(delta_7 / Bevoelkerung * 100000, 1)
) %>%
mutate(
infected_7_per_100k_fill = if_else(infected_7_per_100k < 0, 0, infected_7_per_100k),
infected_7_per_100k_fill = if_else(infected_7_per_100k > max_infections, max_infections + 1, infected_7_per_100k_fill),
delta_7_per_100k_fill = if_else(delta_7_per_100k > max_delta_infections, max_delta_infections + 1, delta_7_per_100k),
delta_7_per_100k_fill = if_else(delta_7_per_100k < -max_delta_infections, -max_delta_infections - 1, delta_7_per_100k_fill),
)
return(data_landkreise_per_day)
}
landkreise <- get_meta_data_landkreise()
geo <- get_geo_data()
corona <- get_initial_landkreis_data(force_refresh = TRUE) %>%
ungroup() %>%
mutate(infected_7_per_100k = replace_na(infected_7_per_100k, 0))
|