-
Notifications
You must be signed in to change notification settings - Fork 2
/
filter_by_pop.R
161 lines (135 loc) · 6.09 KB
/
filter_by_pop.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
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
#!/usr/local/bin/Rscript
# R module to filter data by county/facility, population, & date
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Author: Lauren Chambers
# Update Date: April 2020
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
get_df_by_county <- function(sjc_num_df, population) {
if (population == "p") {
sjc_num_df <- sjc_num_df %>%
dplyr::select(-all_positive, -all_tested) %>%
mutate(all_positive = `N Positive - Detainees/Inmates`,
all_tested = `N Tested - Detainees/Inmates`)
} else if (population == "s") {
sjc_num_df <- sjc_num_df %>%
dplyr::select(-all_positive, -all_tested) %>%
mutate(all_positive = `N Positive - COs` + `N Positive - Staff` + `N Positive - Contractor`,
all_tested = `N Tested - COs` + `N Tested - Staff` + `N Tested - Contractors`)
}
all_df_all <- sjc_num_df %>%
# fix DOC date to be the same as county dates for weekly reports
mutate(Date = if_else(County == "DOC" & Date >= ymd(20200707) & Date < ymd(20201109),
Date + days(1), Date)) %>%
group_by(Date) %>%
summarize(all_released = sum(all_released),
all_positive = sum(all_positive),
all_tested = sum(all_tested),
all_active = sum(`Active Prisoner Cases`)) %>%
mutate(County = "All")
all_df_all_counties <- sjc_num_df %>%
filter(County != "DOC") %>%
group_by(Date) %>%
summarize(all_released = sum(all_released),
all_positive = sum(all_positive),
all_tested = sum(all_tested),
all_active = sum(`Active Prisoner Cases`)) %>%
mutate(County = "All Counties")
df_by_county <- sjc_num_df %>%
mutate(all_active = `Active Prisoner Cases`) %>%
dplyr::select(Date, County, all_positive, all_tested, all_released, all_active) %>%
rbind(all_df_all) %>%
rbind(all_df_all_counties)
return(df_by_county)
}
get_df_by_fac <- function(sjc_num_df, sjc_DOC_num_df, population) {
if (population == "p") {
sjc_num_df <- sjc_num_df %>%
dplyr::select(-all_positive, -all_tested) %>%
mutate(all_positive = `N Positive - Detainees/Inmates`,
all_tested = `N Tested - Detainees/Inmates`)
sjc_DOC_num_df <- sjc_DOC_num_df %>%
dplyr::select(-all_positive, -all_tested) %>%
mutate(all_positive = `N Positive - Detainees/Inmates`,
all_tested = `N Tested - Detainees/Inmates`) %>%
filter(!is.na(all_positive))
} else if (population == "s") {
sjc_num_df <- sjc_num_df %>%
dplyr::select(-all_positive, -all_tested) %>%
mutate(all_positive = `N Positive - COs` + `N Positive - Staff` + `N Positive - Contractor`,
all_tested = `N Tested - COs` + `N Tested - Staff` + `N Tested - Contractors`)
sjc_DOC_num_df <- sjc_DOC_num_df %>%
dplyr::select(-all_positive, -all_tested) %>%
rowwise() %>%
mutate(all_positive = sum(`N Positive - COs`, `N Positive - Other Staff`, na.rm=T),
all_tested = `N Tested - COs`) %>%
ungroup() %>%
filter(!is.na(all_positive))
}
DOC_total_df <- sjc_num_df %>%
filter(County == "DOC") %>%
rename(fac = County) %>%
mutate(fac = "DOC Total**",
all_active = `Active Prisoner Cases`) %>%
dplyr::select(Date, fac, all_positive, all_tested, all_released, all_active)
DOC_fac_total_df <- sjc_DOC_num_df %>%
mutate(all_active = `Active Prisoner Cases`,
all_active = replace_na(all_active, 0)) %>%
group_by(Date) %>%
summarize(all_released = sum(all_released),
all_positive = sum(all_positive),
all_tested = sum(all_tested),
all_active = sum(all_active)) %>%
mutate(fac = "All DOC Facilities")
df_by_fac <- sjc_DOC_num_df %>%
mutate(all_active = `Active Prisoner Cases`) %>%
dplyr::select(Date, fac, all_positive, all_tested, all_released, all_active) %>%
rbind(DOC_total_df) %>%
rbind(DOC_fac_total_df)
return(df_by_fac)
}
get_df_by_county_fac <- function(sjc_num_df, sjc_county_num_df, population) {
if (population == "p") {
sjc_num_df <- sjc_num_df %>%
dplyr::select(-all_positive, -all_tested) %>%
mutate(all_positive = `N Positive - Detainees/Inmates`,
all_tested = `N Tested - Detainees/Inmates`)
sjc_county_num_df <- sjc_county_num_df %>%
dplyr::select(-all_positive, -all_tested) %>%
mutate(all_positive = `N Positive - Detainees/Inmates`,
all_tested = `N Tested - Detainees/Inmates`) %>%
filter(!is.na(all_positive))
} else if (population == "s") {
sjc_num_df <- sjc_num_df %>%
dplyr::select(-all_positive, -all_tested) %>%
mutate(all_positive = `N Positive - COs` + `N Positive - Staff` + `N Positive - Contractor`,
all_tested = `N Tested - COs` + `N Tested - Staff` + `N Tested - Contractors`)
sjc_county_num_df <- sjc_county_num_df %>%
dplyr::select(-all_positive, -all_tested) %>%
group_by(Date, fac) %>%
mutate(all_positive = sum(`N Positive - COs`,
`N Positive - Staff`,
`N Positive - Contractors`, na.rm=T),
all_tested = sum(`N Tested - COs`,
`N Tested - Staff`,
`N Tested - Contractors`, na.rm = T))
}
counties_with_breakdowns <- sjc_county_num_df %>%
separate(fac, c("County", NA), sep=" - ") %>%
pull(County) %>%
unique()
all_df_all_counties <- sjc_num_df %>%
filter(County != "DOC") %>%
group_by(Date) %>%
summarize(all_positive = sum(all_positive),
all_tested = sum(all_tested)) %>%
mutate(fac = "All Counties")
county_total_df <- sjc_num_df %>%
filter(County %in% counties_with_breakdowns) %>%
rename(fac = County) %>%
dplyr::select(Date, fac, all_positive, all_tested)
df_by_county_fac <- sjc_county_num_df %>%
dplyr::select(Date, fac, all_positive, all_tested) %>%
bind_rows(all_df_all_counties) %>%
bind_rows(county_total_df)
return(df_by_county_fac)
}