Is there a R function to calculate the seats in parliament given the total number of seats and the votes for each party -- for different methods including the method of D'Hont? http://en.wikipedia.org/wiki/D%27Hondt_method Thanks, thomas
D'Hondt method
3 messages · Carlos J. Gil Bellosta, Thomas Steiner
Hello,
I believe that a "productionized" version of the following would do:
dHont <- function( candidates, votes, seats ){
tmp <- data.frame(
candidates = rep( candidates, each = seats ),
scores = as.vector(sapply( votes, function(x) x /
1:seats ))
)
tmp <- tmp$candidates[order( - tmp$scores )] [1:seats]
table(tmp)
}
votes <- sample(1:10000, 5) votes
[1] 448 7685 5445 482 6266
dHont(letters[1:5], votes, 10 )
tmp a b c d e 0 4 3 0 3 Best regards, Carlos J. Gil Bellosta http://www.datanalytics.com
On Wed, 2009-02-04 at 12:16 +0100, Thomas Steiner wrote:
Is there a R function to calculate the seats in parliament given the total number of seats and the votes for each party -- for different methods including the method of D'Hont? http://en.wikipedia.org/wiki/D%27Hondt_method Thanks, thomas
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
very cool, easier than I thought... (although I am not fully familiar with sapply() and friends) quick reality-check (no proof!) at http://icon.cat/util/elections/isuHqUyDYh shows that the results coincide: votes <- c(42201,38635,247736,170627,48236,117151,61379,35889,92321) dHont(c("BGLD","KTN","N?","O?","SLBG","STMK","T","VLBG","W"), votes, 26) so thanks a lot, have a great day! Thomas