Skip to content

Instantly share code, notes, and snippets.

@smach
Created March 27, 2014 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smach/9798663 to your computer and use it in GitHub Desktop.
Save smach/9798663 to your computer and use it in GitHub Desktop.
Sample R script for finding winners in a proportional election system
# Sample R script for finding winners in a proportional election system
# by Sharon Machlis
# This example assumes a spreadsheet with a list of candidates with column headers
# Municipality, Party, Place, Name, Variable
# and a spreadsheet with a list of results with column headers
# Municipality, Red, Blue, Green (Red, Blue, Green being sample political party names)
# See sample spreadsheet format at
# https://onedrive.live.com/redir?resid=7DDBA42BF10D288B!662&authkey=!AAFnW1H3wGiZgQU&ithint=file%2c.xlsx
# Read in data from spreadsheets: candidates list and election results
library("xlsx")
candidates <- read.xlsx("test.xlsx", sheetName = "candidates", stringsAsFactors=FALSE)
results <- read.xlsx("test.xlsx", sheetName = "results", stringsAsFactors=FALSE)
########## USING data.tables ###############
library(data.table)
# Create data tables dtcandidates and dtresults from the data frames we just made above
dtcandidates <- data.table(candidates)
dtresults <- data.table(results)
# setkey tells data.table which column variables we want to search or group by
setkey(dtcandidates, Municipality, Party, Place)
setkey(dtresults, Municipality)
# This function takes a municipality name and returns which candidates won seats
getwinners <- function(muni){
numwinners <- dtresults[muni] # Gets the municipality's row from the results spreadsheet
numwinners$Municipality <- NULL # Deletes the Municipality column since we don't need that
# Find all the parties that had at least 1 winner
winningparties <- apply(numwinners, 2, function(x) x > 0)
parties <- subset(numwinners, select=winningparties)
# Get a vector of names of winning parties (just column names from parties)
partynames <- names(parties)
# Create an empty data frame to store info about each winning candidate
myresults <<- data.frame(Municipality=character(0), Party=character(0), Place=integer(0), Name=character(0), Variable=character(0))
# This loop goes through each party that won seats and finds winning candidates for each party
for(p in partynames){
# Extract number of seats won by the party:
themax <- as.numeric(subset(parties, select=p))
# Find all the candidates from that party with a place less than or equal to number of winners
elected <- dtcandidates[J(muni, p, 1:themax)]
# Add those rows to the originally empty data frame we created above
myresults <<- rbind(myresults, elected)
}
return(myresults)
}
# Here, store all the municipalities you have results for in the dtresults data.table
cities <- dtresults$Municipality
# Run the getwinners function on each one of the cities
myresults_list <- lapply(cities, getwinners)
# Turn the list of results in myresults_list into a single data.table:
myresultsdt <- do.call(rbind, myresults_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment