Skip to content

Commit

Permalink
commit function
Browse files Browse the repository at this point in the history
  • Loading branch information
kalashsinghal committed May 8, 2024
1 parent fb60f57 commit 4d42d85
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions R/best_and_worst_cases.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
best_and_worst_cases <- function(data) {
data |>
mutate(amount_of_distinct_products = dplyr::n_distinct(.data$clustered), .by = "companies_id") |>
mutate(equal_weight = (1/.data$amount_of_distinct_products)) |>
lowest_risk_category_per_company(.by = "companies_id") |>
highest_risk_category_per_company(.by = "companies_id") |>
mutate(dummy_best = ifelse(.data$risk_category == .data$min_risk_category_per_company, 1, 0)) |>
mutate(dummy_worst = ifelse(.data$risk_category == .data$max_risk_category_per_company, 1, 0)) |>
mutate(count_best_case_products_per_company_benchmark = sum(.data$dummy_best), .by = c("companies_id", "grouped_by")) |>
mutate(count_worst_case_products_per_company_benchmark = sum(.data$dummy_worst), .by = c("companies_id", "grouped_by")) |>
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)) |>
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))
}

lowest_risk_category_per_company <- function(data, .by) {
risk_order <- c("low", "medium", "high")
mutate(data, min_risk_category_per_company = risk_order[which(risk_order %in% .data$risk_category)[1]], .by = all_of(.by))
}

highest_risk_category_per_company <- function(data, .by) {
risk_order <- c("high", "medium", "low")
mutate(data, max_risk_category_per_company = risk_order[which(risk_order %in% .data$risk_category)[1]], .by = all_of(.by))
}

0 comments on commit 4d42d85

Please sign in to comment.