Learn R Programming

redistmetrics

redistmetrics is one of the R packages developed and maintained by the ALARM Project. redistmetrics provides the back-end for the computation of summary statistics for a redistricting plan. It provides a more direct access point to use methods in redist without requiring redist objects.

Installation

You can install the stable version of redistmetrics from CRAN with:

install.packages('redistmetrics')

You can install the development version of redistmetrics from GitHub with:

if (!requireNamespace('remotes')) install.packages('remotes')
remotes::install_github('alarm-redist/redistmetrics')

Example

library(redistmetrics)

redistmetrics offers support for 4 common input types and has examples of each, all based on New Hampshire:

data(nh)

This example is based on comp_polsby() for the Polsby Popper compactness, but comp_polsby() can be substituted for any implemented measure!

Single Plan:

For a single plan, we can pass the single plan to the input. We also pass an argument to shp which takes in an sf dataframe. r_2020 here is the Republican proposal for New Hampshire’s congressional districts.

comp_polsby(plans = nh$r_2020, shp = nh)
#> [1] 0.2324375 0.1582763

The output here is a numeric vector, where each entry is the output for a district. The first district here has a compactness of about 0.23 and the second district has a compactness of about 0.16.

Now, if you’re redistricting in R, we recommend using the R package redist. In which case, you would have a redist_map object.

We can load an example here with:

data(nh_map)

For redist maps, the workflow is identical!

comp_polsby(plans = nh_map$r_2020, shp = nh)
#> [1] 0.2324375 0.1582763

Multiple Plans:

For multiple plans, we can pass either a matrix of plans or a redist_plans object to plans. We will still need nh or nh_map to provide the shapes.

If we have a matrix, we can compare with nh_m a matrix of plans, where each column indicates a plan.

data(nh_m)

From there, the process is nearly identical. Here we compute the Polsby Popper compactness for the first two columns:

comp_polsby(plans = nh_m[, 1:2], shp = nh)
#> [1] 0.1844955 0.1796426 0.2324375 0.1582763

Now we got 4 outputs: 1 for each district x 2 for each plan x 2 plans.

If we are using redist, we likely have a redist_plans object which hides the matrix as an attribute to give a more familiar tidy workflow. With that, we can do a very similar process:

First, we load the plans object (included as an example):

data(nh_plans)

The benefit of using a redist_plans object is that we can cleanly mutate into it using the . shortcut:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
nh_plans <- nh_plans %>% mutate(polsby = comp_polsby(plans = ., shp = nh))
#> Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE

Now our values are cleanly held in the redist_plans object:

head(nh_plans)
#> # A tibble: 6 × 4
#>   draw   district total_pop polsby
#>   <fct>     <int>     <dbl>  <dbl>
#> 1 d_2020        1    688739  0.184
#> 2 d_2020        2    688790  0.180
#> 3 r_2020        1    688676  0.232
#> 4 r_2020        2    688853  0.158
#> 5 1             1    688961  0.235
#> 6 1             2    688568  0.349

Detailed information on each measure are contained in the vignettes and references are contained in the function documentation.

Copy Link

Version

Install

install.packages('redistmetrics')

Monthly Downloads

444

Version

1.0.11

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Christopher T. Kenny

Last Published

December 15th, 2025

Functions in redistmetrics (1.0.11)

comp_ch

Calculate Convex Hull Compactness
compet_talisman

Compute Talismanic Redistricting Competitiveness Metric
comp_skew

Calculate Skew Compactness
dist_info

Calculate Variation of Information Distances
comp_schwartz

Calculate Schwartzberg Compactness
dist_euc

Calculate Euclidean Distances
comp_polsby

Calculate Polsby Popper Compactness
dist_ham

Calculate Hamming Distances
comp_reock

Calculate Reock Compactness
comp_y_sym

Calculate Y Symmetry Compactness
nh_m

Redistricting Plans for New Hampshire as matrix
nh_plans

Redistricting Plans for New Hampshire as redist_plans
comp_x_sym

Calculate X Symmetry Compactness
part_decl

Calculate Declination
part_bias

Calculate Partisan Bias
part_decl_simple

Calculate Simplified Declination
dist_man

Calculate Manhattan Distances
inc_pairs

Count Incumbent Pairings
nh_map

New Hampshire Election and Demographic Data as a redist_map
part_dil_asym

Calculate Dilution Asymmetry
part_rmd

Calculate Ranked Marginal Deviation
part_dislocation

Calculate Partisan Dislocation
list_fn

Return Functions Matching a Prefix
part_resp

Calculate Responsiveness
part_egap

Calculate Efficiency Gap
splits_admin

Compute Number of Administrative Units Split
nh

New Hampshire Election and Demographic Data
tally

Tally a Column by District
splits_total

Count the Total Splits in Each Plan
seg_dissim

Compute Dissimilarity Index
splits_multi

Compute Number of Administrative Units Split More than Once
part_dseats

Calculate Democratic Seats
part_egap_ep

Calculate Efficiency Gap (Equal Population Assumption)
splits_count

Count the Number of Splits in Each Administrative Unit
splits_district_fuzzy

Fuzzy Splits by District (Experimental)
splits_sub_count

Count the Number of Splits in Each Sub-Administrative Unit
%>%

Pipe operator
prep_perims

Prep Polsby Popper Perimeter Tibble
part_dvs

Calculate Democratic Vote Share
splits_sub_total

Count the Total Sub-Administrative Unit in Each Plan
part_mean_median

Calculate Mean Median Score
part_sscd

Calculate Smoothed Seat Count Deviation
part_tau_gap

Calculate Tau Gap
part_lop_wins

Calculate Lopsided Wins
splits_sub_admin

Compute Number of Sub-Administrative Units Split
comp_bbox_reock

Calculate Bounding Box Reock Compactness
comp_frac_kept

Calculate Fraction Kept Compactness
comp_bc

Calculate Boyce Clark Ratio
comp_log_st

Calculate Log Spanning Tree Compactness
comp_fh

Calculate Fryer Holden Compactness
by_plan

Shorten District by Plan vector
comp_lw

Calculate Length Width Compactness
comp_box_reock

Calculate Box Reock Compactness
comp_edges_rem

Calculate Edges Removed Compactness