-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions_aggregation.R
63 lines (52 loc) · 2.93 KB
/
functions_aggregation.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
if ('dplyr' %in% devtools::loaded_packages()$package == F) { library(dplyr) }
case_aggregator <- function(dat) {
case_filed_date_var <- colnames(dat)[grepl(x = colnames(dat), pattern = 'date')]
dat[[case_filed_date_var]] <- as.Date(dat[[case_filed_date_var]], format = '%Y-%m-%d')
dat
}
defendant_aggregator <- function(dat) {
case_id_var <- colnames(dat)[grepl(x = colnames(dat), pattern = 'id')]
name_var <- colnames(dat)[grepl(x = colnames(dat), pattern = 'name')]
address_var <- colnames(dat)[grepl(x = colnames(dat), pattern = 'address')]
order_var <- colnames(dat)[grepl(x = colnames(dat), pattern = 'order')]
dat <- dat %>% group_by(eval(as.symbol(case_id_var))) %>%
arrange(eval(as.symbol(order_var)), .by_group = TRUE) %>%
mutate(defendant_count = n(),
all_defendant_names = paste0(eval(as.symbol(name_var)), collapse = ' | '),
all_defendant_addresses = paste0(eval(as.symbol(address_var)), collapse = ' | '),
defendant_order_validation = paste0(eval(as.symbol(order_var)), collapse = ' | ')) %>%
filter(eval(as.symbol(order_var)) == 1) %>% ungroup()
dat <- dat %>% select(-contains(c('judgment', 'eval', eval(order_var))))
dat
}
plaintiff_aggregator <- function(dat) {
case_id_var <- colnames(dat)[grepl(x = colnames(dat), pattern = 'id')]
name_var <- colnames(dat)[grepl(x = colnames(dat), pattern = 'plaintiff_name')] # `plaintiff_name` instead of `name` to distinguish from `clean_party_name`
address_var <- colnames(dat)[grepl(x = colnames(dat), pattern = 'address')]
order_var <- colnames(dat)[grepl(x = colnames(dat), pattern = 'order')]
dat <- dat %>% group_by(eval(as.symbol(case_id_var))) %>%
arrange(eval(as.symbol(order_var)), .by_group = TRUE) %>%
mutate(plaintiff_count = n(),
all_plaintiff_names = paste0(eval(as.symbol(name_var)), collapse = ' | '),
all_plaintiff_addresses = paste0(eval(as.symbol(address_var)), collapse = ' | '),
plaintiff_order_validation = paste0(eval(as.symbol(order_var)), collapse = ' | ')) %>%
filter(eval(as.symbol(order_var)) == 1) %>% ungroup()
dat <- dat %>% select(-contains(c('judgment', 'eval', eval(order_var))))
dat
}
hearing_aggregator <- function(dat, date_format = '%m/%d/%Y', time_format = '%R %p') {
case_id_var <- colnames(dat)[grepl(x = colnames(dat), pattern = 'id')]
dat <- dat %>% group_by(eval(as.symbol(case_id_var))) %>%
mutate(date = as.Date(date, format = '%m/%d/%Y')) %>%
arrange(desc(date)) %>%
summarize(hearing_count = n(),
disposition_date = date[result %in% c("Judgment", "Default Judgment", "Other")][1],
disposition = result[result %in% c("Judgment", "Default Judgment", "Other")][1]) %>%
ungroup()
dat <- rename_with(dat, function(x) sub('^.+$', {{case_id_var}}, x), contains('eval'))
dat
}
judgment_aggregator <- function(dat) {
dat <- dat %>% select(everything(), judgment = starts_with('judgment'))
dat
}