You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R中按客户分组,用上一单元格结果计算EAD列?

修正R中按分组递推计算EAD的代码

你的核心问题是accumulate的调用没有结合每个分组的初始Vol值,仅计算了BRD的累积乘积,导致结果不符合预期。以下是两种可行的修正方案:

方案一:使用accumulate结合初始值

library(dplyr)
library(purrr)

df <- data.frame(Cust = c(1,1,1,2,2,2), 
                 Rec_count = c(1,2,3,1,2,3), 
                 BRD = c(1,0.9,0.9, 1, 0.8, 0.8), 
                 T_y = c(2,2,2,1,1,1), 
                 Vol = c(100,100,100,80,80,80))

df1 <- df %>%
  group_by(Cust) %>%
  # 以分组的第一个Vol为初始值,对BRD做累积乘积,去掉多余的初始值
  mutate(EAD = accumulate(BRD, ~ .x * .y, .init = first(Vol)) %>% tail(-1)) %>%
  ungroup()

print(df1)

方案二:用case_when+fill递推填充

library(dplyr)
library(tidyr)

df <- data.frame(Cust = c(1,1,1,2,2,2), 
                 Rec_count = c(1,2,3,1,2,3), 
                 BRD = c(1,0.9,0.9, 1, 0.8, 0.8), 
                 T_y = c(2,2,2,1,1,1), 
                 Vol = c(100,100,100,80,80,80))

df1 <- df %>%
  group_by(Cust) %>%
  # 先给Rec_count=1的行赋值Vol,其余行定义递推规则
  mutate(EAD = case_when(
    Rec_count == 1 ~ Vol,
    TRUE ~ lag(EAD) * BRD
  )) %>%
  # 向下填充递推计算缺失值
  fill(EAD, .direction = "down") %>%
  ungroup()

print(df1)

结果验证

两种方案都会得到符合预期的EAD列:

CustRec_countBRDT_yVolEAD
111.02100100.0
120.9210090.0
130.9210081.0
211.018080.0
220.818064.0
230.818051.2

原代码问题说明

原代码中accumulate(BRD[1:n()], function(x, y) x * y)仅对BRD列做了累积乘积,没有将每个分组的初始Vol值作为计算起点,因此得到的是BRD的累积结果(如1、0.9、0.81),而非基于Vol的递推值。

内容的提问来源于stack exchange,提问作者Manfred

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 09:54:14