如何修改此R代码以使结果无序?座位分配

时间:2017-09-14 14:18:49

标签: r

我找到了一个R套餐SiencesPo,使用不同的方法按比例分配席位。 https://www.rdocumentation.org/packages/SciencesPo/versions/1.4.1/topics/HighestAverages

我对函数HighestAverages感兴趣,但提供的结果是一个频率表,根据分配给每一方的席位数重新排序输出。

我想得到一个保持原始结果的向量。

原因是因为我想在许多不同的地区应用它,然后汇总结果。

例如

party=c("White", "Yellow", "Red", "Green", "Blue", "Pink")
votes=c(16000,47000, 15900, 12000,  6000,   3100)
HighestAverages(party, votes, seats = 10, method="dh")

产生这个结果:

-------------------------
 Party   Seats   % Seats 
------- ------- ---------
Yellow     5       50    
  Red      2       20    
 White     2       20    
 Green     1       10    
 Blue      0        0    
 Pink      0        0    

我想保留原始订单,只是

-------------------------
 Party   Seats   % Seats 
------- ------- ---------
 White     2       20    
Yellow     5       50    
  Red      2       20    
 Green     1       10    
 Blue      0        0    
 Pink      0        0    

或者更好的只是一个矢量:

 c(2,5,2,1,0,0)

我该如何修改代码? 或者如何修改输出以获得我需要的输出?

HighestAverages <-   function(parties = NULL, votes = NULL, 
    seats = NULL, method = c("dh"), threshold = 0) {
    .ratio <- votes / sum(votes)
    .votes <- ifelse(.ratio < threshold, 0, votes)

    # To deal with  NULL party labels
    if (is.null(parties)) {      parties <- replicate(length(votes),paste(sample(LETTERS, 3,replace = TRUE), collapse = ""))     }

    # Define Quotient, I only need "dh"
    switch(
      method,
      dh = {  divisor.vec <- seq(from = 1,by = 1, length.out = seats) ; method.name <- c("d'Hondt") }
    )

    # ratio = as.vector(sapply(votes, function(x) x / sum(votes)))
    .temp <- data.frame(parties = rep(parties, each = seats), scores = as.vector(sapply(.votes, function(x) x / divisor.vec)))

    out <- with(.temp, (parties[order(-scores)][1:seats]))  #
    output <- freq(out, digits = 3, perc = TRUE)

    # Political diversity indices
    ENP_votes <- 1 / sum(.ratio ^ 2)
    ENP_seats <- 1 / sum((output$Freq / sum(output$Freq)) ^ 2)
    LSq_index <- sqrt(0.5 * sum(((( votes / sum(votes) ) * 100) - ((output$Freq / sum(output$Freq)) * 100  )) ^ 2))

    .shorten <- function(x, n)       cat("Divisors:", x[1:n], "...", "\n")
    cat("Method:", method.name, "\n")
    .shorten(round(divisor.vec, 2), 4)
    cat(paste("ENP:", round(ENP_votes, 2), "(After):", round(ENP_seats, 2)), "\n")
    cat(paste("Gallagher Index: ", round(LSq_index, 2)), "\n \n")
    names(output) <- c("Party", "Seats", "\u0025 Seats")

    # output <- output[ order(output[,2], decreasing = TRUE),]
    class(output) <- c("SciencesPo", class(output))
    attr(output, "scpo.type") <- "Standard"
    return(output)
  }

}

我猜重要的行是

out <- with(.temp, (parties[order(-scores)][1:seats]))  #
output <- freq(out, digits = 3, perc = TRUE)

但我不知道如何在没有副作用的情况下正确修改它们。

1 个答案:

答案 0 :(得分:1)

如果将str()应用于HighestAverages函数的结果,那么您将知道它是data.frame的子类。 因此,您可以使用data.frame修改它。

library(SciencesPo)

party=c("White", "Yellow", "Red", "Green", "Blue", "Pink")
votes=c(16000,47000, 15900, 12000,  6000,   3100)
x <- HighestAverages(party, votes, seats = 10, method="dh")

str(x)

x$Party <- factor(x$Party, levels=party)
x <- x[order(x$Party),]
rownames(x) <- NULL   
x