Skip to content

Commit 4d42d85

Browse files
committed
commit function
1 parent fb60f57 commit 4d42d85

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

R/best_and_worst_cases.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
best_and_worst_cases <- function(data) {
2+
data |>
3+
mutate(amount_of_distinct_products = dplyr::n_distinct(.data$clustered), .by = "companies_id") |>
4+
mutate(equal_weight = (1/.data$amount_of_distinct_products)) |>
5+
lowest_risk_category_per_company(.by = "companies_id") |>
6+
highest_risk_category_per_company(.by = "companies_id") |>
7+
mutate(dummy_best = ifelse(.data$risk_category == .data$min_risk_category_per_company, 1, 0)) |>
8+
mutate(dummy_worst = ifelse(.data$risk_category == .data$max_risk_category_per_company, 1, 0)) |>
9+
mutate(count_best_case_products_per_company_benchmark = sum(.data$dummy_best), .by = c("companies_id", "grouped_by")) |>
10+
mutate(count_worst_case_products_per_company_benchmark = sum(.data$dummy_worst), .by = c("companies_id", "grouped_by")) |>
11+
mutate(best_case = ifelse(.data$count_best_case_products_per_company_benchmark == 0, NA, .data$dummy_best/.data$count_best_case_products_per_company_benchmark)) |>
12+
mutate(worst_case = ifelse(.data$count_worst_case_products_per_company_benchmark == 0, NA, .data$dummy_worst/.data$count_worst_case_products_per_company_benchmark))
13+
}
14+
15+
lowest_risk_category_per_company <- function(data, .by) {
16+
risk_order <- c("low", "medium", "high")
17+
mutate(data, min_risk_category_per_company = risk_order[which(risk_order %in% .data$risk_category)[1]], .by = all_of(.by))
18+
}
19+
20+
highest_risk_category_per_company <- function(data, .by) {
21+
risk_order <- c("high", "medium", "low")
22+
mutate(data, max_risk_category_per_company = risk_order[which(risk_order %in% .data$risk_category)[1]], .by = all_of(.by))
23+
}

0 commit comments

Comments
 (0)