对于特定的数据帧,我需要在我的数据帧上分配一定量(POT)。当该数量耗尽时,它应该返回剩余的数据帧而不分配值。输入DF:
Accepted Jackpot Commision Rank
NO 2760.33 279.85 1
NO 2760.33 279.85 1
NO 2760.33 279.85 1
NO 2760.33 279.85 1
NO 2760.33 206.38 2
NO 2760.33 206.38 2
NO 2760.33 206.38 2
正如您所看到的,DF有4列,但实际上更大,但这些是重要的列。我希望在根据Rank列消耗佣金时,将Accepted更改为Yes。
因此,累积奖金 - 佣金(2760.33 - 279.85 = 2480.48)对于排名为1的所有行,将“已接受”变为“是”。累积奖金现在为:2480.48。所以这就是我们可以用于Rank 2等等。直到我们达到0并简单所有接受留在NO。
Accepted Jackpot Commision Rank
YES 2760.33 279.85 1
YES 2760.33 279.85 1
YES 2760.33 279.85 1
YES 2760.33 279.85 1
NO 2480.48 206.38 2
NO 2480.48 206.38 2
NO 2480.48 206.38 2
我尝试了很多像By, (s)apply, for, aggregate
这样的东西,但是我没有设法得到正确的答案。任何帮助表示赞赏。
完整的测试数据框:
dput(tst_df)
structure(list(Accepted = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1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ot = c(2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33, 2760.33,
2760.33, 2760.33, 2760.33, 2760.33, 2760.33), Commision = c(12.71,
12.71, 12.71, 27.52, 27.52, 27.52, 27.52, 27.52, 16.94, 16.94,
16.94, 31.51, 31.51, 31.51, 31.51, 31.51, 3.72, 3.72, 16.68,
16.68, 16.68, 14.46, 14.46, 132.13, 132.13, 132.13, 132.13,
132.13, 132.13, 132.13, 132.13, 132.13, 132.13, 132.13, 132.13,
132.13, 132.13, 132.13, 132.13, 132.13, 132.13, 132.13, 132.13,
132.13, 132.13, 132.13, 132.13, 12.19, 12.19, 6.82, 6.82,
55.31, 55.31, 55.31, 55.31, 55.31, 55.31, 55.31, 13.01, 13.01,
155.8, 155.8, 155.8, 155.8, 155.8, 155.8, 155.8, 155.8, 155.8,
155.8, 155.8, 155.8, 155.8, 155.8, 155.8, 155.8, 155.8, 155.8,
155.8, 155.8, 155.8, 155.8, 155.8, 155.8, 155.8, 155.8, 155.8,
155.8, 155.8, 155.8, 279.85, 279.85, 279.85, 279.85, 279.85,
279.85, 279.85, 279.85, 279.85, 279.85, 279.85, 279.85, 279.85,
279.85, 279.85, 279.85, 279.85, 279.85, 279.85, 279.85, 279.85,
279.85, 279.85, 279.85, 279.85, 279.85, 279.85, 279.85, 279.85,
279.85, 279.85, 279.85, 279.85, 279.85, 279.85, 279.85, 279.85,
279.85, 279.85, 279.85, 279.85, 279.85, 1.86, 6.2, 6.2, 36.96,
36.96, 36.96, 36.96, 36.96, 36.96, 36.96, 36.96, 36.96, 9.3,
6.45, 8.26, 10.16, 10.16, 10.16, 10.16, 6.61, 5.78, 6.2,
18.59, 18.59, 18.59, 84.24, 84.24, 84.24, 84.24, 84.24, 84.24,
84.24, 84.24, 84.24, 84.24, 84.24, 31.19, 31.19, 31.19, 31.19,
31.19, 31.19, 167.78, 167.78, 167.78, 167.78, 167.78, 167.78,
167.78, 167.78, 167.78, 167.78, 167.78, 167.78, 167.78, 167.78,
167.78, 167.78, 167.78, 167.78, 167.78, 167.78, 167.78, 167.78,
167.78, 167.78, 167.78, 167.78, 167.78, 167.78, 167.78, 1.24,
3.81, 3.81, 4.13, 17.67, 17.67, 17.67, 17.67, 106.72, 106.72,
106.72, 106.72, 106.72, 106.72, 106.72, 106.72, 106.72, 106.72,
106.72, 106.72, 106.72, 106.72, 106.72, 106.72, 106.72, 9.3,
55.43, 55.43, 55.43, 55.43, 55.43, 55.43, 55.43, 2.23, 24.79,
24.79, 24.79, 24.79, 260.22, 260.22, 260.22, 260.22, 260.22,
260.22, 260.22, 260.22, 260.22, 260.22, 260.22, 260.22, 260.22,
260.22, 260.22, 260.22, 260.22, 260.22, 260.22, 260.22, 260.22,
260.22, 260.22, 260.22, 260.22, 260.22, 260.22, 260.22, 260.22,
260.22, 260.22, 3.51, 3.51, 3.51, 7.44, 4.29, 4.29, 6.61,
27.58, 27.58, 27.58, 27.58, 27.58, 27.58, 27.58, 115.08,
115.08, 115.08, 115.08, 115.08, 115.08, 115.08, 115.08, 115.08,
115.08, 115.08, 115.08, 115.08, 115.08, 115.08, 115.08, 206.38,
206.38, 206.38, 206.38, 206.38, 206.38, 206.38, 206.38, 206.38,
206.38, 206.38, 206.38, 206.38, 206.38, 206.38, 206.38, 206.38,
206.38, 206.38, 206.38, 206.38, 206.38, 206.38, 206.38, 206.38,
206.38, 206.38, 206.38, 206.38, 206.38, 206.38, 206.38, 206.38,
206.38, 206.38, 206.38, 69.1, 69.1, 69.1, 69.1, 69.1, 69.1,
69.1, 69.1, 69.1, 69.1, 69.1, 69.1, 69.1, 4.63, 0, 6.69,
6.69, 6.69, 6.69, 66.85, 66.85, 66.85, 66.85, 66.85, 66.85,
66.85, 66.85, 66.85, 11.98), Rank = c(15L, 15L, 15L, 18L,
18L, 18L, 18L, 18L, 37L, 37L, 37L, 25L,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 5L, 5L, 5L, 5L, 66L, 66L, 66L, 79L, 45L,
45L, 46L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 104L, 26L, 29L, 29L, 29L, 29L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 57L)), .Names = c("Accepted", "Jackpot",
"Commision", "Rank"), row.names = c(NA, -367L), class = "data.frame")
答案 0 :(得分:1)
在这个数据集中,似乎“累积奖金”没有耗尽,因此解决方案是微不足道的。也就是说,
library(dplyr)
tst_df %>%
distinct() %>%
arrange(Rank) %>%
mutate(cumsum = cumsum(Commision), remaining = Jackpot - cumsum)
# Accepted Jackpot Commision Rank cumsum remaining
# 1 NO 2760.33 279.85 1 279.85 2480.48
# 2 NO 2760.33 206.38 2 486.23 2274.10
# 3 NO 2760.33 155.80 3 642.03 2118.30
# 4 NO 2760.33 132.13 4 774.16 1986.17
# <snip>
# 48 NO 2760.33 13.01 70 2172.54 587.79
# 49 NO 2760.33 7.44 79 2179.98 580.35
# 50 NO 2760.33 9.30 85 2189.28 571.05
# 51 NO 2760.33 4.63 104 2193.91 566.42
假设我们将“Jackpot”设置为较小的数字。解决方案是找出金额耗尽的Rank
,然后将汇总表加入原始数据集:
new_df <- mutate(tst_df, Jackpot = 1000)
accepted <- new_df %>%
distinct() %>%
arrange(Rank) %>%
mutate(cumsum = cumsum(Commision),
remaining = Jackpot - cumsum,
Accepted = if_else(remaining > 0, "YES", "NO"))
head(accepted)
# Accepted Jackpot Commision Rank cumsum remaining
# 1 YES 1000 279.85 1 279.85 720.15
# 2 YES 1000 206.38 2 486.23 513.77
# 3 YES 1000 155.80 3 642.03 357.97
# 4 YES 1000 132.13 4 774.16 225.84
# 5 NO 1000 260.22 5 1034.38 -34.38
# 6 NO 1000 115.08 6 1149.46 -149.46
所以这里所有{4}或以下的Rank
都Accepted
等于“是”。最后
out <- left_join(select(accepted, Accepted, Rank), select(tst_df, -Accepted))