Skip to content

Instantly share code, notes, and snippets.

@ramnathv
Created December 9, 2013 19:25
Show Gist options
  • Save ramnathv/7879235 to your computer and use it in GitHub Desktop.
Save ramnathv/7879235 to your computer and use it in GitHub Desktop.
library(RCurl)
url = getURL("https://raw.github.com/tcrug/ranked-choice-vote-data/master/2013-mayor-cvr.csv")
vote = read.csv(text = url, stringsAsFactors = F)
vote = vote[ , 2:4]
vote[vote == "undervote"] = NA
vote[vote == "overvote"] = NA
names(vote) = paste('choice', 1:3, sep = "_")
vote2 = apply(vote, 1, as.list)
find_loser <- function(l){
l1 = sapply(l, '[[', 'choice_1')
attr(which.min(table(l1)), 'names')
}
slide_choices <- function(l, loser){
lapply(l, function(x){
x2 = Filter(function(y) !(y == loser), x)
return(x2)
})
}
replace_loser <- function(l, loser){
l[l == loser]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment