R-供应周数

R-供应周数,r,generate-series,R,Generate Series,我试图计算一个有1000万行的数据集的销售预测,手头的库存将持续多少周。我在下面给出的数据结构的最后一列中列出了预期的输出。我还附上了这个在Excel中的实现 Logic Weeksofsupply = Number of weeks the current inventory on hand will last. example - in the attached image (SKU_CD 222, STORE_CD 33), the inventory on hand is 19, the

我试图计算一个有1000万行的数据集的销售预测,手头的库存将持续多少周。我在下面给出的数据结构的最后一列中列出了预期的输出。我还附上了这个在Excel中的实现

Logic
Weeksofsupply = Number of weeks the current inventory on hand will last.
example - in the attached image (SKU_CD 222, STORE_CD 33), the inventory on hand is 19, the sales values are
WK1 + WK2 = 15, Wk1 + Wk2 + Wk3 = 24, Which is greater than 19, 
So we are picking 2, which the count of Weeks the current inventory will last.
最后一列中的预期输出

当前Excel代码:(此处周以列形式显示,但应为预期输出中显示的行。)


=IF(A2这里有一种方法,使用线性插值法
approxfun

data$WeeksSupply <- sapply(1:nrow(data),function(i)
                     approxfun(cumsum(as.vector(c(data[i,2:11]))),1:10)(data$Inventory[i]))
data$WeeksSupply <- replace(data$WeeksSupply,is.na(data$WeeksSupply),
                            "Inventory Exceeds Forecast")

data
# A tibble: 2 x 12
  Inventory   Wk1   Wk2   Wk3   Wk4   Wk5   Wk6   Wk7   Wk8   Wk9  Wk10                WeeksSupply
      <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>                      <chr>
1       200    20    15    25    40    35    45    30    50    45    55           6.66666666666667
2      2000    20    15    25    40    35    45    30    50    45    55 Inventory Exceeds Forecast

data$WeeksSupply对于长格式的修订数据,您可以执行以下操作

library(dplyr) #for the grouping functionality

#define a function to calculate weeks Supply from Sales and Inventory
weekSup <- function(sales,inv){
  sales <- unlist(sales)
  inv <- unlist(inv)
  n <- length(sales)
  weeksup <- rep(NA,n)
  for(i in 1:n){
    if(i==n | inv[i]<sales[i]){
      weeksup[i] <- ifelse(inv[i]>sales[i],NA,inv[i]/sales[i])
    } else {
      weeksup[i] <- approxfun(cumsum(sales[i:n]),1:(n-i+1))(inv[i])
    }
  }
  #Your 'inventory more' is coded as -1 (a number) to avoid whole column being forced to a character vector
  weeksup <- replace(weeksup,is.na(weeksup),-1)
  return(weeksup) #for whole weeks, change this to `return(floor(weeksup))`
}

Data2 <- Data %>% group_by(SKU_CD,STORE_CD) %>% mutate(weekSup=weekSup(SALES,INVENTORY))

head(Data2,20)

   SKU_CD STORE_CD FWK_CD SALES INVENTORY  WeeksofSupply    weekSup
    <dbl>    <dbl>  <dbl> <dbl>     <dbl>          <chr>      <dbl>
 1    111       22 201627     5        29             11 11.3333333
 2    111       22 201628     2        27             10 10.8333333
 3    111       22 201629     2        25              9  9.8333333
 4    111       22 201630     2        23              8  8.8333333
 5    111       22 201631     1        22              8  8.0000000
 6    111       22 201632     3        19              6  6.6666667
 7    111       22 201633     2        17              5  5.8333333
 8    111       22 201634     2        15              4  4.8333333
 9    111       22 201635     3        12              3  3.6666667
10    111       22 201636     2        10              2  2.8333333
11    111       22 201637     3        25 Inventory More -1.0000000
12    111       22 201638     6         1 Inventory Less  0.1666667
13    222       33 201627     7        19              2  2.4444444
14    222       33 201628     8        17              2  2.0000000
15    222       33 201629     9        15              1  1.6000000
16    222       33 201630    10        13              1  1.2727273
17    222       33 201631    11        12              1  1.0833333
18    222       33 201632    12         9 Inventory Less  0.7500000
19    222       33 201633    13         7 Inventory Less  0.5384615
20    222       33 201634    14         5 Inventory Less  0.3571429
library(dplyr)#用于分组功能
#定义一个函数,用于根据销售和库存计算供应周数

weekSup谢谢你。关于如何将周作为一个专栏而不是多个专栏来实现这一点,有什么想法吗?非常感谢你的意见!我已经将其作为第二个答案发布了。非常感谢!这非常有帮助。
data$WeeksSupply <- sapply(1:nrow(data),function(i)
                     approxfun(cumsum(as.vector(c(data[i,2:11]))),1:10)(data$Inventory[i]))
data$WeeksSupply <- replace(data$WeeksSupply,is.na(data$WeeksSupply),
                            "Inventory Exceeds Forecast")

data
# A tibble: 2 x 12
  Inventory   Wk1   Wk2   Wk3   Wk4   Wk5   Wk6   Wk7   Wk8   Wk9  Wk10                WeeksSupply
      <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>                      <chr>
1       200    20    15    25    40    35    45    30    50    45    55           6.66666666666667
2      2000    20    15    25    40    35    45    30    50    45    55 Inventory Exceeds Forecast
library(dplyr) #for the grouping functionality

#define a function to calculate weeks Supply from Sales and Inventory
weekSup <- function(sales,inv){
  sales <- unlist(sales)
  inv <- unlist(inv)
  n <- length(sales)
  weeksup <- rep(NA,n)
  for(i in 1:n){
    if(i==n | inv[i]<sales[i]){
      weeksup[i] <- ifelse(inv[i]>sales[i],NA,inv[i]/sales[i])
    } else {
      weeksup[i] <- approxfun(cumsum(sales[i:n]),1:(n-i+1))(inv[i])
    }
  }
  #Your 'inventory more' is coded as -1 (a number) to avoid whole column being forced to a character vector
  weeksup <- replace(weeksup,is.na(weeksup),-1)
  return(weeksup) #for whole weeks, change this to `return(floor(weeksup))`
}

Data2 <- Data %>% group_by(SKU_CD,STORE_CD) %>% mutate(weekSup=weekSup(SALES,INVENTORY))

head(Data2,20)

   SKU_CD STORE_CD FWK_CD SALES INVENTORY  WeeksofSupply    weekSup
    <dbl>    <dbl>  <dbl> <dbl>     <dbl>          <chr>      <dbl>
 1    111       22 201627     5        29             11 11.3333333
 2    111       22 201628     2        27             10 10.8333333
 3    111       22 201629     2        25              9  9.8333333
 4    111       22 201630     2        23              8  8.8333333
 5    111       22 201631     1        22              8  8.0000000
 6    111       22 201632     3        19              6  6.6666667
 7    111       22 201633     2        17              5  5.8333333
 8    111       22 201634     2        15              4  4.8333333
 9    111       22 201635     3        12              3  3.6666667
10    111       22 201636     2        10              2  2.8333333
11    111       22 201637     3        25 Inventory More -1.0000000
12    111       22 201638     6         1 Inventory Less  0.1666667
13    222       33 201627     7        19              2  2.4444444
14    222       33 201628     8        17              2  2.0000000
15    222       33 201629     9        15              1  1.6000000
16    222       33 201630    10        13              1  1.2727273
17    222       33 201631    11        12              1  1.0833333
18    222       33 201632    12         9 Inventory Less  0.7500000
19    222       33 201633    13         7 Inventory Less  0.5384615
20    222       33 201634    14         5 Inventory Less  0.3571429