代码之家  ›  专栏  ›  技术社区  ›  av abhishiek

根据R中的条件,最近四周的平均销售额

  •  0
  • av abhishiek  · 技术社区  · 6 年前

    我有特定周内促销产品的数据集,我想计算过去4个非促销周(用标志表示)从促销到非促销期间产品的平均销售额。如果交易是在非促销期内进行的,我们应该照原样进行销售。我们必须计算最近非促销周的平均销售额,它们可能是非连续的。

    请注意

    structure(list(Product_group = structure(c(1L, 1L, 1L, 1L, 1L, 
    1L), .Label = "A", class = "factor"), Promo = structure(c(1L, 
    1L, 2L, 1L, 1L, 2L), .Label = c("0", "1"), class = "factor"), 
        Week = structure(c(1L, 2L, 2L, 3L, 4L, 5L), .Label = c("2017-01-01", 
        "2017-01-02", "2017-01-04", "2017-01-05", "2017-01-06", "2017-01-08", 
        "2017-01-09"), class = "factor"), Sales = c(50, 50, 60, 70, 
        50, 50)), .Names = c("Product_group", "Promo", "Week", "Sales"
    ), row.names = c(NA, 6L), class = "data.frame")
    
    head(df)
      Product_group Promo       Week Sales
    1             A     0 2017-01-01    50
    2             A     0 2017-01-02    50
    3             A     1 2017-01-02    60
    4             A     0 2017-01-04    70
    5             A     0 2017-01-05    50
    6             A     1 2017-01-06    50
    

    我正在寻找这样的输出

              Product_group Promo       Week Sales Avg Pre Promo Sales
        1             A     0 2017-01-01    50      50 # Since it is non promo
        2             A     0 2017-01-02    50      50 
        3             A     1 2017-01-02    60      50 # 100/2
        4             A     0 2017-01-04    70      70 
        5             A     0 2017-01-05    50      50
        6             A     1 2017-01-06    50      55 # (50 +70 + 50 + 50 )/4
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   kangaroo_cliff    6 年前

    什么时候 Promo == 1 ,我查看 Promo 为零。然后选择最近四周的最大值以获得平均值。

      df <- rbind(df, df) # get more rows to data
    
      df$AvgPrePromoSales <- 
       sapply(1 : nrow(df), function(x) if(df$Promo[x] == 1) {
            ind <- which(df[1:x,]$Promo == 0)
            mean(df$Sales[ind[max(1, length(ind) - 3) : length(ind)]])
          } else {
            df$Sales[x] 
          })
    
    df[, c(2, 4, 5)]
    #      Promo Sales AvgPrePromoSales
    # 1      0    50               50
    # 2      0    50               50
    # 3      1    60               50
    # 4      0    70               70
    # 5      0    50               50
    # 6      1    50               55
    # 7      0    50               50
    # 8      0    50               50
    # 9      1    60               55
    # 10     0    70               70
    # 11     0    50               50
    # 12     1    50               55