我怎样才能按百分比向量分配数字向量,对结果进行四舍五入,并始终得到与我在 R 中开始时相同的总数?

How can I distribute a vector of numbers by a vector of percentages, round the result, and always get the same total that I started with in R?

问题总结

我想将数字向量(Sum_By_Group 列)乘以百分比向量(Percent 列),以将组的总数分配到每个 ID,四舍五入结果,并最终得到与我开始时相同的总数。换句话说,我希望 Distribution_Post_Round 列与 Sum_By_Group 列相同。

下面是我 运行 遇到的问题的示例。在 A 组中,我将 Percent 乘以 Sum_By_Group,最后 ID 1 中有 3 个,ID 2 中有 3 个,ID 5 中有 1 个,总共 7 个。Sum_By_Group 列和 Distribution_Post_Round 列与 A 组相同,这就是我想要的。在 B 组中,我将 Percent 乘以 Sum_By_Group 并以 ID 8 中的 1 和 ID 10 中的 1 结束,总共 2。我希望 B 组的 Distribution_Post_Round 列为 3 .

有没有办法在不使用循环、子集数据帧然后将数据帧重新连接在一起的情况下做到这一点?

例子

library(dplyr)
df = data.frame('Group' = c(rep('A', 7), rep('B', 5)),
                  'ID' = c(1:12),
                  'Percent' = c(0.413797750, 0.385366840, 0.014417571, 0.060095668, 0.076399650,
                                0.019672573, 0.030249949, 0.381214519, 0.084121796, 0.438327886,
                                0.010665749, 0.085670050),
                  'Sum_By_Group' = c(rep(7,7), rep(3, 5)))
df$Distribute_By_ID = round(df$Percent * df$Sum_By_Group, 0)
df_round = aggregate(Distribute_By_ID ~ Group, data = df, sum)
names(df_round)[names(df_round) == 'Distribute_By_ID'] = 'Distribution_Post_Round'
df = left_join(df, df_round, by = 'Group')
df
  Group ID    Percent Sum_By_Group Distribute_By_ID Distribution_Post_Round
      A  1 0.41379775            7                3                       7
      A  2 0.38536684            7                3                       7
      A  3 0.01441757            7                0                       7
      A  4 0.06009567            7                0                       7
      A  5 0.07639965            7                1                       7
      A  6 0.01967257            7                0                       7
      A  7 0.03024995            7                0                       7
      B  8 0.38121452            3                1                       2
      B  9 0.08412180            3                0                       2
      B 10 0.43832789            3                1                       2
      B 11 0.01066575            3                0                       2
      B 12 0.08567005            3                0                       2

非常感谢您的帮助。如果需要进一步说明,请告诉我。

哇,谁知道有人已经写了一个包含解决这个问题的函数的包...kudos to that team https://cran.r-project.org/web/packages/sfsmisc/index.html

既然你似乎愿意使用 dplyr 希望这个额外的包是值得的,因为它确实使解决方案变得优雅。

# 
library(dplyr)

df = data.frame('Group' = c(rep('A', 7), rep('B', 5)),
                'ID' = c(1:12),
                'Percent' = c(0.413797750, 0.385366840, 0.014417571, 0.060095668, 0.076399650,
                              0.019672573, 0.030249949, 0.381214519, 0.084121796, 0.438327886,
                              0.010665749, 0.085670050),
                'Sum_By_Group' = c(rep(7,7), rep(3, 5)))

glimpse(df)
#> Rows: 12
#> Columns: 4
#> $ Group        <chr> "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "…
#> $ ID           <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
#> $ Percent      <dbl> 0.41379775, 0.38536684, 0.01441757, 0.06009567, 0.076399…
#> $ Sum_By_Group <dbl> 7, 7, 7, 7, 7, 7, 7, 3, 3, 3, 3, 3

df %>% 
  group_by(Group) %>% 
  mutate(Distribute_By_ID = sfsmisc::roundfixS(Percent * Sum_By_Group))
#> # A tibble: 12 x 5
#> # Groups:   Group [2]
#>    Group    ID Percent Sum_By_Group Distribute_By_ID
#>    <chr> <int>   <dbl>        <dbl>            <dbl>
#>  1 A         1  0.414             7                3
#>  2 A         2  0.385             7                3
#>  3 A         3  0.0144            7                0
#>  4 A         4  0.0601            7                0
#>  5 A         5  0.0764            7                1
#>  6 A         6  0.0197            7                0
#>  7 A         7  0.0302            7                0
#>  8 B         8  0.381             3                1
#>  9 B         9  0.0841            3                0
#> 10 B        10  0.438             3                2
#> 11 B        11  0.0107            3                0
#> 12 B        12  0.0857            3                0

reprex package (v0.3.0)

于 2020-05-07 创建
df %>%
  mutate(dividend = floor(Percent*Sum_By_Group),
         remainder= Percent*Sum_By_Group-dividend) %>%
  group_by(Group) %>%
  arrange(desc(remainder),.by_group=TRUE) %>%
  mutate(delivered=sum(dividend),
         rownumber=1:n(),
         lastdelivery=if_else(rownumber<=Sum_By_Group-delivered,1,0),
         Final=dividend+lastdelivery) %>%
  ungroup()

# A tibble: 12 x 10
   Group    ID Percent Sum_By_Group dividend remainder delivered rownumber lastdelivery Final
   <fct> <int>   <dbl>        <dbl>    <dbl>     <dbl>     <dbl>     <int>        <dbl> <dbl>
 1 A         1  0.414             7        2    0.897          4         1            1     3
 2 A         2  0.385             7        2    0.698          4         2            1     3
 3 A         5  0.0764            7        0    0.535          4         3            1     1
 4 A         4  0.0601            7        0    0.421          4         4            0     0
 5 A         7  0.0302            7        0    0.212          4         5            0     0
 6 A         6  0.0197            7        0    0.138          4         6            0     0
 7 A         3  0.0144            7        0    0.101          4         7            0     0
 8 B        10  0.438             3        1    0.315          2         1            1     2
 9 B        12  0.0857            3        0    0.257          2         2            0     0
10 B         9  0.0841            3        0    0.252          2         3            0     0
11 B         8  0.381             3        1    0.144          2         4            0     1
12 B        11  0.0107            3        0    0.0320         2         5            0     0

这是我的解决方案,没有依赖 Hare quota 的任何其他依赖项: 我分配了所有整数"seats",然后我按照余数的顺序分配了剩余的"seats"。 然后 "Final" 列就可以了。

注意:它似乎给出了与其他带有包的解决方案相同的结果

将其表述为整数优化问题:

library(CVXR)
A <- as.data.frame.matrix(t(model.matrix(~0+Group, df)))
prop <- df$Percent * df$Sum_By_Group
x <- Variable(nrow(df), integer=TRUE)
sums <- df$Sum_By_Group[!duplicated(df$Group)]
p <- Problem(Minimize(sum_squares(x - prop)), list(A %*% x == sums))
result <- solve(p)

df$Distribute_By_ID <- as.integer(round(result$getValue(x)))

输出:

   Group ID    Percent Sum_By_Group
1      A  1 0.41379775            7
2      A  2 0.38536684            7
3      A  3 0.01441757            7
4      A  4 0.06009567            7
5      A  5 0.07639965            7
6      A  6 0.01967257            7
7      A  7 0.03024995            7
8      B  8 0.38121452            3
9      B  9 0.08412180            3
10     B 10 0.43832789            3
11     B 11 0.01066575            3
12     B 12 0.08567005            3