gpt4 book ai didi

r - split apply combine w/function 或 purrr package pmap?

转载 作者:行者123 更新时间:2023-12-02 01:17:24 25 4
gpt4 key购买 nike

这是我要解决的大问题。如果我有足够的声誉来奖励我,我会的!

寻求平衡销售代表客户的地域。我把这个过程分解了,我真的不知道如何在每个地区进行。

在此示例中,有 4 个区域的 1000 个帐户,每个区域有 2 个子联盟,然后是帐户的不同所有者 -- 有些帐户是无主的。每个帐户都有一个介于 1,000 和 100,000 之间的随机值。

可重现的例子:

账户列表:

set.seed(1)
Accounts <- paste0("Acc", 1:1000)
Region <- c("NorthEast", "SouthEast", "MidWest", "West")
League <- sample(c("Majors", "Minors"), 1000, replace = TRUE)
AccValue <- sample(1000:100000, 1000, replace = TRUE)
Owner <- sample(c("Chad", NA, "Jimmy", "Adrian", NA, NA, "Steph", "Matt", "Jared", "Eric"), 1000, replace = TRUE)
AccDF <- data.frame(Accounts, Region, League, AccValue, Owner)
AccDF$Accounts <- as.character(AccDF$Accounts)
AccDF$Region <- as.character(AccDF$Region)
AccDF$League <- as.character(AccDF$League)
AccDF$Owner <- as.character(AccDF$Owner)

地区所有权总结:

Summary <- AccDF %>%
group_by(Region, League, Owner) %>%
summarise(Count = n(),
TotalValue = sum(AccValue))

按地区、联赛汇总:

Summary2 <- AccDF %>%
group_by(Region, League) %>%
summarise(Count = n(),
TotalValue = sum(AccValue),
AccountsPerRep = round(Count / 7, 0),
ValuePerRep = TotalValue / 7)

这就是所有的起始数据,我想对 Summary2 表的每个分组进行以下处理。

西部未成年人示例:

西部未成年人帐户总数:120

#break out into owned and unowned

WestMinorsOwned <- AccDF %>%
filter(Region == "West",
League == "Minors",
!is.na(Owner))

WestMinorsUnowned <- AccDF %>%
filter(Region == "West",
League == "Minors",
is.na(Owner))

#unassign accounts until threshold is hit

New.WestMinors <- WestMinorsOwned %>%
mutate(r = runif(n())) %>%
arrange(r) %>%
group_by(Owner) %>%
mutate(NewOwner = replace(Owner, cumsum(AccValue) > 600000 | row_number() > 14, NA)) %>%
ungroup(Owner) %>%
mutate(Owner = NewOwner) %>%
select(-r, -NewOwner)

所有者更新后,我们将各个部分重新绑定(bind)在一起以拥有 WestMinors 帐户库,所有这些都具有更新的所有者,希望是平衡的。

AssignableWestMinors <- bind_rows(filter(AccDF, Region == "West" & League == "Minors" & is.na(Owner)), 
filter(New.WestMinors, is.na(Owner))) %>%
arrange(desc(AccValue))

#check work
OwnerSummary <- New.WestMinors %>%
filter(!is.na(Owner)) %>%
group_by(Region, League, Owner) %>%
summarise(Count = n(), TotalValue = sum(AccValue))

没有人拥有超过 14 个或 600,000 个帐户,因此我们可以开始重新分配无主帐户以尝试平衡每个人。下面的 for 循环查看 OwnerSummary 中的每个名称,找出分配给他们的最小 $$ 并分配最有值(value)的帐户,然后遍历每个帐户,试图平衡每个所有者的份额。

#Balance Unassigned

for (i in 1:nrow(AssignableWestMinors)){
idx <- which.min(OwnerSummary$TotalValue)
OwnerSummary$TotalValue[idx] <- OwnerSummary$TotalValue[idx] + AssignableWestMinors$AccValue[i]
OwnerSummary$Count[idx] <- OwnerSummary$Count[idx] + 1
AssignableWestMinors$Owner[i] <- as.character(OwnerSummary$Owner[idx])}

现在我们只需将之前拥有的和新分配的绑定(bind)在一起,我们就完成了平衡的 West Minors 领土。

WestMinors.Final <- bind_rows(filter(New.WestMinors, !is.na(Owner)), AssignableWestMinors)

WM.Summary <- WestMinors.Final %>%
group_by(Region, League, Owner) %>%
summarise(Count = n(),
TotalValue = sum(AccValue))

每个人的账户数量都差不多,总的 $$ 领土都在合理范围内。

现在,我正在尝试为最初的 4 个地区、2 个联赛的每个分组执行此操作。所以这样做 8 次,然后将它们缝合在一起。每个子组都有不同的 $$ 值(value)阈值和帐户数量。我怎样才能将原始帐户基础分成 8 个部分,应用所有这些,然后将它们重新组合在一起?

最佳答案

您应该利用 ?dplyr::do 对 Region-League 的子集执行您想要的拆分-应用-组合操作。首先,功能化您的逻辑,使其可以在数据框 dta 上运行,该数据框表示主数据框 AccDF 的子集版本。

reAssign <- function(dta) {
other_acct <- dta %>%
filter(!is.na(Owner)) %>%
mutate(r = runif(n())) %>%
arrange(r) %>%
group_by(Owner) %>%
mutate(NewOwner = replace(Owner, cumsum(AccValue) > 600000 | row_number() > 14, NA)) %>%
ungroup(Owner) %>%
mutate(Owner = NewOwner) %>%
select(-r, -NewOwner)

assignable_acct <- other_acct %>%
filter(is.na(Owner)) %>%
bind_rows( filter(dta, is.na(Owner)) ) %>%
arrange(desc(AccValue))

acct_summary <- other_acct %>%
filter(!is.na(Owner)) %>%
group_by(Owner) %>%
summarise(Count = n(), TotalValue = sum(AccValue))

# I have a feeling there's a much better way of doing this, but oh well...
for (i in seq(nrow(assignable_acct))) {
idx <- which.min(acct_summary$TotalValue)
acct_summary$TotalValue[idx] <- acct_summary$TotalValue[idx] + assignable_acct$AccValue[i]
acct_summary$Count[idx] <- acct_summary$Count[idx] + 1
assignable_acct$Owner[i] <- as.character(acct_summary$Owner[idx])
}
final <- other_acct %>%
filter(!is.na(Owner)) %>%
bind_rows(assignable_acct)

return(final)
}

然后简单地将它应用到已经按地区、联赛分组的 AccDF。

new_master <- AccDF %>% 
group_by(Region, League) %>%
do( reAssign(.) ) %>%
ungroup()

检查以确保它完成了它的工作...

new_master %>% 
group_by(Region, League, Owner) %>%
summarise(Count = n(),
TotalValue = sum(AccValue)) %>%
as.data.frame()

关于r - split apply combine w/function 或 purrr package pmap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42094520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com