generated from RMI-PACTA/workflow.template.pacta
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add prepare_sector_split.R #30
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c0e34bb
add prepare_sector_split.R
jacobvjk f39c112
Merge branch 'main' into 9-prepare-sector-split
jacobvjk 5e77d5a
Merge branch 'main' into 9-prepare-sector-split
jacobvjk 90eaa25
add optional sector split to match_prioritize
jacobvjk 50bbf4e
Merge branch 'main' into 9-prepare-sector-split
jacobvjk cb6f2de
clarify sector split script
jacobvjk bf37671
Merge branch 'main' into 9-prepare-sector-split
jacobvjk 646ca7b
simplify
jacobvjk c99824c
remove unnecessary dependencies
jacobvjk 3ea94d8
clean up helper functions
jacobvjk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
lost_companies_sector_split <- function(abcd, | ||
companies_sector_split) { | ||
abcd_id <- abcd %>% | ||
dplyr::distinct(.data$company_id, .data$name_company) | ||
|
||
# identify lost_companies_sector_split and write to csv for inspection | ||
lost_companies_sector_split <- companies_sector_split %>% | ||
dplyr::anti_join( | ||
abcd_id, | ||
by = c("company_id") | ||
) | ||
Comment on lines
+6
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: single line pipe |
||
|
||
return(lost_companies_sector_split) | ||
} | ||
|
||
apply_sector_split_to_loans <- function(data, | ||
abcd, | ||
companies_sector_split) { | ||
unique_companies_pre_split <- data %>% | ||
distinct(name_abcd) | ||
|
||
abcd_id <- abcd %>% | ||
dplyr::distinct(.data$company_id, .data$name_company) | ||
|
||
companies_sector_split <- companies_sector_split %>% | ||
dplyr::left_join( | ||
abcd_id, | ||
by = c("company_id") | ||
) %>% | ||
dplyr::select(-"company_id") | ||
|
||
data <- data %>% | ||
dplyr::inner_join( | ||
companies_sector_split, | ||
by = c("name_abcd" = "name_company", "sector_abcd" = "sector") | ||
) %>% | ||
dplyr::mutate( | ||
# renaming the loan_id is not conditional to avoid any chance of accidentally | ||
# renaming a split loan to a loan_id that already exists elsewhere | ||
id_loan = paste(.data$id_loan, .data$sector_abcd, sep = "_"), | ||
loan_size_outstanding = dplyr::if_else( | ||
is.na(.data$sector_split), | ||
.data$loan_size_outstanding, | ||
.data$loan_size_outstanding * .data$sector_split | ||
), | ||
loan_size_credit_limit = dplyr::if_else( | ||
is.na(.data$sector_split), | ||
.data$loan_size_credit_limit, | ||
.data$loan_size_credit_limit * .data$sector_split | ||
) | ||
) %>% | ||
dplyr::select(-"sector_split") | ||
|
||
unique_companies_post_split <- data %>% | ||
distinct(name_abcd) | ||
|
||
if (nrow(unique_companies_pre_split) != nrow(unique_companies_post_split)) { | ||
warning( | ||
glue::glue( | ||
"Applying the sector split has lead to changes in the number of unique | ||
companies covered in the analysis. Prior to the split, there were | ||
{nrow(unique_companies_pre_split)} unique companies. After the split, | ||
there are {nrow(unique_companies_post_split)} unique companies." | ||
) | ||
) | ||
} | ||
|
||
return(data) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: single line pipe (i don't really care that much, just making note)