Compactness Calculation Tool

mandeR calculates a variety of compactness measures, which are useful for detecting and analyzing gerrymandering of electoral districts. Beneath the hood, it relies on compactnesslib.

Example

The following example loads some data into R and then calculates compactness scores for that data. GeoJSON is used to communicate with the underlying compactnesslib.

#Load library
library(mandeR)

#Read a shapefile containing districts
dists  <- sf::st_read(mass_cd())

#Get the number of districts in the shapefile
ndists <- length(st_geometry(dists))

#Variable to hold scores
scores <- NA

for(i in 1:ndists){
  #Get the WKT for a district
  wkt_str <- st_as_text(st_geometry(dists)[[i]])

  #Retrieve compactness scores from mandeR
  tempscores <- mandeR::getScoresForWKT(wkt_str)

  #Set the ID of the district in the return
  tempscores$id <- dists$DIST_NUM[[i]]

  #Add scores to list
  scores <- rbind(scores,tempscores)
}

#Merge scores back into districts
dists  <- merge(dists, scores, by.x="DIST_NUM", by.y="id")

#Plot districts showing each of the scores
plot(dists[mandeR::getListOfScores()])

Citing this Package

Please cite this package using the text returned by:

citation('mandeR')


gerrymandr/mandeR documentation built on May 26, 2019, 6:35 a.m.