[R] D'Hondt method

Carlos J. Gil Bellosta
2009-02-04T13:11:18Z

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.