Helper function to aggregate a vector by district. Can be used to calculate total population, group percentages, and more.

tally(plans, shp, x)

Arguments

plans

A redist_plans object or plans_matrix where each row indicates a district assignment and each column is a plan.

shp

A redist_map object, tibble, or data frame containing other columns.

x

The numeric vector to tally.

Value

A numeric vector with the tallies. Can be shaped into a district-by-plan matrix.

Examples

data(nh)
data(nh_m)

tally(nh_m, nh, pop) # total population
#>   [1] 688739 688790 688676 688853 688961 688568 688936 688593 689040 688489
#>  [11] 688489 689040 688755 688774 688724 688805 688964 688565 688801 688728
#>  [21] 688439 689090 688960 688569 689040 688489 688964 688565 688581 688948
#>  [31] 688489 689040 688494 689035 688936 688593 688685 688844 688982 688547
#>  [41] 688781 688748 688494 689035 689076 688453 688768 688761 688599 688930
#>  [51] 688685 688844 688610 688919 688781 688748 688685 688844 688842 688687
#>  [61] 688614 688915 688476 689053 688832 688697 688755 688774 688510 689019
#>  [71] 688773 688756 688489 689040 688516 689013 688684 688845 688494 689035
#>  [81] 688736 688793 688827 688702 688827 688702 688439 689090 688768 688761
#>  [91] 688689 688840 688656 688873 688656 688873 688689 688840 688474 689055
#> [101] 688662 688867 688516 689013
tally(nh_m, nh, vap_hisp) / tally(nh_m, nh, vap) # HVAP
#>   [1] 0.03512070 0.03590870 0.03626437 0.03477678 0.02015796 0.05138086
#>   [7] 0.04864204 0.02257190 0.04474267 0.02651330 0.04181488 0.02918947
#>  [13] 0.04467132 0.02637827 0.02127072 0.05018002 0.04052334 0.03050798
#>  [19] 0.02069914 0.05077635 0.03278563 0.03827131 0.02206072 0.04922579
#>  [25] 0.04474267 0.02651330 0.04052334 0.03050798 0.04885263 0.02240665
#>  [31] 0.04181488 0.02918947 0.02145553 0.04984817 0.04864204 0.02257190
#>  [37] 0.02065734 0.05072914 0.03415595 0.03686542 0.02529303 0.04579502
#>  [43] 0.02145553 0.04984817 0.05071206 0.02079216 0.04224537 0.02891570
#>  [49] 0.02273181 0.04857129 0.02065734 0.05072914 0.03200872 0.03908279
#>  [55] 0.02529303 0.04579502 0.02065734 0.05072914 0.05120431 0.02029671
#>  [61] 0.04994760 0.02138244 0.02053322 0.05094016 0.05153105 0.01997581
#>  [67] 0.04467132 0.02637827 0.03188723 0.03919601 0.02080290 0.05064604
#>  [73] 0.04181488 0.02918947 0.02257181 0.04860699 0.02040345 0.05077663
#>  [79] 0.02145553 0.04984817 0.03180647 0.03924126 0.03338110 0.03764726
#>  [85] 0.03338110 0.03764726 0.03278563 0.03827131 0.04224537 0.02891570
#>  [91] 0.04043100 0.03067914 0.02591528 0.04528326 0.02591528 0.04528326
#>  [97] 0.04043100 0.03067914 0.02221721 0.04918104 0.04835107 0.02299177
#> [103] 0.02257181 0.04860699