You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 line
2.5KB

  1. donations_from_receipts <- function(
  2. receipts,
  3. addresses_out = NULL,
  4. receipt_type_code = c("IND", "CPCM", "GEN", "PPTY", "OUTS", "NFPC"),
  5. exclude_org_name = c("AGGREGATED INDIVIDUAL CONTRIBUTION", "VARIOUS VARIOUS")
  6. ) {
  7. if (is.character(receipts)) {
  8. receipts <- prep_open_dataset_db(receipts)
  9. }
  10. if (!is.null(addresses_out) && is.character(addresses_out)) {
  11. addresses <- out_open_dataset_db(addresses_out)
  12. }
  13. ret <-
  14. receipts |>
  15. filter(
  16. # Keep individual/party donors; drop record keeping things, like refunds
  17. receipt_type_code %in% !!receipt_type_code,
  18. !toupper(org_name) %in% !!exclude_org_name
  19. ) |>
  20. rename(donor_name = org_name) |>
  21. # address lookup is the key for matching with the resolved addresses db
  22. add_address_lookup(name = "donor_address_raw") |>
  23. select(
  24. sboe_id,
  25. report_id,
  26. donor_name,
  27. donor_address_raw,
  28. amount,
  29. profession,
  30. employers_name,
  31. form_of_payment_desc,
  32. everything()
  33. )
  34. if (is.null(addresses_out)) return(ret)
  35. ret |>
  36. left_join(
  37. addresses |> select(1:2),
  38. by = join_by(donor_address_raw == address_lookup)
  39. ) |>
  40. rename(donor_address = address_resolved) |>
  41. relocate(donor_address, .after = donor_name) |>
  42. mutate(donor_address = coalesce(donor_address, !!fixup_po_box_query(donor_address_raw)))
  43. }
  44. count_receipt_types <- function(receipts) {
  45. # # A tibble: 13 × 3
  46. # receipt_type_code receipt_type_desc n
  47. # <chr> <chr> <dbl>
  48. # 1 IND Individual Contribution 7425756
  49. # 2 CPCM Other Political Committee Contribution 78354
  50. # 3 GEN General Contribution 65692
  51. # 4 PPTY Party Contribution 27623
  52. # 5 OUTS Outside Source 16415
  53. # 6 INT Interest Earned 10308
  54. # 7 RFND Refund/Reimbursement to the Committee 7353
  55. # 8 CNRE Contribution to be Reimbursed 3057
  56. # 9 EPPS Exempt Party Price Sale 685
  57. # 10 NFPC Not for Profit Contribution 628
  58. # 11 DON Donation 28
  59. # 12 DEBT Debt Payment 4
  60. # 13 GNS Goods and Services 1
  61. #
  62. receipts |>
  63. count(receipt_type_code, receipt_type_desc, sort = TRUE)
  64. }