首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在没有显式for循环的组和行上应用递归函数

在没有显式for循环的组和行上应用递归函数
EN

Stack Overflow用户
提问于 2020-03-05 00:13:19
回答 1查看 131关注 0票数 1

对标题表示歉意;我不知道如何表达这个问题,所以我将举例说明,并希望提供一个reprex。

假设我有以下数据框架:

代码语言:javascript
复制
tmp <- data.frame(Campaign = rep(c("Toy", "Clothing"), each = 10), 
                  CPC = rep(c(6.40, 10.30), each = 10), 
                  CompoundGrowth = rep(c(0.005, 0.0123), each = 10), 
                  Month = rep(seq(as.Date("2013-01-01"), 
                                  as.Date("2013-01-01") + months(9), by = "month"), 2)
                 )
OUT:
-----------------------------------------------------------
    Campaign  CPC CompoundGrowth      Month
1       Toy  6.4         0.0050 2013-01-01
2       Toy  6.4         0.0050 2013-02-01
3       Toy  6.4         0.0050 2013-03-01
4       Toy  6.4         0.0050 2013-04-01
5       Toy  6.4         0.0050 2013-05-01
6       Toy  6.4         0.0050 2013-06-01
7       Toy  6.4         0.0050 2013-07-01
8       Toy  6.4         0.0050 2013-08-01
9       Toy  6.4         0.0050 2013-09-01
10      Toy  6.4         0.0050 2013-10-01
11 Clothing 10.3         0.0123 2013-01-01
12 Clothing 10.3         0.0123 2013-02-01
13 Clothing 10.3         0.0123 2013-03-01
14 Clothing 10.3         0.0123 2013-04-01
15 Clothing 10.3         0.0123 2013-05-01
16 Clothing 10.3         0.0123 2013-06-01
17 Clothing 10.3         0.0123 2013-07-01
18 Clothing 10.3         0.0123 2013-08-01
19 Clothing 10.3         0.0123 2013-09-01
20 Clothing 10.3         0.0123 2013-10-01

我想添加一个列ProjCPC

代码语言:javascript
复制
ProjCPC = ProjCPC + ProjCPC * CompoundGrowth 

第一行将是

代码语言:javascript
复制
CPC + CPC * CompoundGrowth. 

换句话说,一个递归函数

代码语言:javascript
复制
f(x) = f(x)*c + f(x).

我真的希望这件事能成功

代码语言:javascript
复制
projection_func <- function(current_projection, cpc_growth){
    current_projection = current_projection * cpc_growth + current_projection
    return(current_projection)
}

tmp %>% dplyr::group_by(Campaign, Month) %>% 
    dplyr::rowwise() %>%
    mutate(CPC = ifelse(is.na(lag(CPC)), projection_func(CPC, CompoundGrowth),
                                      projection_func(lag(CPC), CompoundGrowth)
    )
)

但它只做了第一次计算:

代码语言:javascript
复制
   Campaign   CPC CompoundGrowth Month     
   <fct>    <dbl>          <dbl> <date>    
 1 Toy       6.43         0.005  2013-01-01
 2 Toy       6.43         0.005  2013-02-01
 3 Toy       6.43         0.005  2013-03-01
 4 Toy       6.43         0.005  2013-04-01
 5 Toy       6.43         0.005  2013-05-01
 6 Toy       6.43         0.005  2013-06-01
 7 Toy       6.43         0.005  2013-07-01
 8 Toy       6.43         0.005  2013-08-01
 9 Toy       6.43         0.005  2013-09-01
10 Toy       6.43         0.005  2013-10-01
11 Clothing 10.4          0.0123 2013-01-01
12 Clothing 10.4          0.0123 2013-02-01
13 Clothing 10.4          0.0123 2013-03-01
14 Clothing 10.4          0.0123 2013-04-01
15 Clothing 10.4          0.0123 2013-05-01
16 Clothing 10.4          0.0123 2013-06-01
17 Clothing 10.4          0.0123 2013-07-01
18 Clothing 10.4          0.0123 2013-08-01
19 Clothing 10.4          0.0123 2013-09-01
20 Clothing 10.4          0.0123 2013-10-01

我期望的产出:

代码语言:javascript
复制
   Campaign  expected CompoundGrowth      Month
1       Toy  6.432000         0.0050 2013-01-01
2       Toy  6.464160         0.0050 2013-02-01
3       Toy  6.496481         0.0050 2013-03-01
4       Toy  6.528963         0.0050 2013-04-01
5       Toy  6.561608         0.0050 2013-05-01
6       Toy  6.594416         0.0050 2013-06-01
7       Toy  6.627388         0.0050 2013-07-01
8       Toy  6.660525         0.0050 2013-08-01
9       Toy  6.693828         0.0050 2013-09-01
10      Toy  6.727297         0.0050 2013-10-01
11 Clothing 10.426690         0.0123 2013-01-01
12 Clothing 10.554938         0.0123 2013-02-01
13 Clothing 10.684764         0.0123 2013-03-01
14 Clothing 10.816187         0.0123 2013-04-01
15 Clothing 10.949226         0.0123 2013-05-01
16 Clothing 11.083901         0.0123 2013-06-01
17 Clothing 11.220233         0.0123 2013-07-01
18 Clothing 11.358242         0.0123 2013-08-01
19 Clothing 11.497948         0.0123 2013-09-01
20 Clothing 11.639373         0.0123 2013-10-01

用于产生上述内容的代码(试图避免这种解决方案):

代码语言:javascript
复制
for(i in seq_along(tmp$Campaign)){
    if(i%%10 %in% c(1,10)){
        starter = projection_func(tmp$CPC[i], tmp$CompoundGrowth[i])
    }
    else{
        starter = projection_func(starter, tmp$CompoundGrowth[i])
    }
    tmp$expected[i] <- starter
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-05 00:43:51

由于每个计算都依赖于以前计算的结果,因此可以使用来自accumalatepurrr

代码语言:javascript
复制
library(dplyr)

tmp %>%
  group_by(Campaign) %>%
  mutate(ProjCPC = purrr::accumulate(CompoundGrowth, projection_func, 
                  .init = first(CPC))[-1]) 

#   Campaign  CPC CompoundGrowth      Month   ProjCPC
#1       Toy  6.4         0.0050 2013-01-01  6.432000
#2       Toy  6.4         0.0050 2013-02-01  6.464160
#3       Toy  6.4         0.0050 2013-03-01  6.496481
#4       Toy  6.4         0.0050 2013-04-01  6.528963
#5       Toy  6.4         0.0050 2013-05-01  6.561608
#6       Toy  6.4         0.0050 2013-06-01  6.594416
#7       Toy  6.4         0.0050 2013-07-01  6.627388
#8       Toy  6.4         0.0050 2013-08-01  6.660525
#9       Toy  6.4         0.0050 2013-09-01  6.693828
#10      Toy  6.4         0.0050 2013-10-01  6.727297
#11 Clothing 10.3         0.0123 2013-01-01 10.426690
#12 Clothing 10.3         0.0123 2013-02-01 10.554938
#13 Clothing 10.3         0.0123 2013-03-01 10.684764
#14 Clothing 10.3         0.0123 2013-04-01 10.816187
#15 Clothing 10.3         0.0123 2013-05-01 10.949226
#16 Clothing 10.3         0.0123 2013-06-01 11.083901
#17 Clothing 10.3         0.0123 2013-07-01 11.220233
#18 Clothing 10.3         0.0123 2013-08-01 11.358242
#19 Clothing 10.3         0.0123 2013-09-01 11.497948
#20 Clothing 10.3         0.0123 2013-10-01 11.639373
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60536499

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档