【问题标题】:Rolling stock distribution in RR中的机车车辆分布
【发布时间】:2020-11-11 06:04:09
【问题描述】:

我有一个数据框,其中包含按月的手头库存、到货数量和预期消费量。

我需要计算每个月需要购买的数量,以使我的库存每月保持在一定安全库存之上。

例如:我有 3 件库存,4 件第一个月到货,预计消费量为 0。因此我这个月不需要购买任何件,剩下 3+4-0=7。

这意味着下个月我将从 7 开始,假设消费量为 7 并且到达 1 单位。因此,我的安全库存量为 4 时,我将不足 3 件,因此我需要在当月计划购买 3 件。

第 3 个月,我从 4 件安全库存开始,1 件到货,消耗 5 件,所以我是 0,所以我需要编程购买 4 件来补充我的安全库存。

作为一个例子,我需要复制以下“购买”是我需要计算的地方

df <- data.frame(type = c("a","a","a","a","a"),
date = as.Date(c("2020-01-01", "2020-02-01", "2020-03-01", "2020-04-01", "2020-05-01")),
stock= c(3,0,0,0,0),
arriving = c(4,1,1,3,2),
consumption = c(0,7,5,5,3),
safety_stock = c(4,4,4,4,4),
to_purchase= c(0,3,4,2,1))

【问题讨论】:

  • 如果你下个月从 7 开始,我认为你的 df$stock[2] 应该是 7,而不是 0。这是你在那个月开始的初始库存单位。因此,您的stock 值应为df$stock = c(3, 7, 4, 4, 4),以反映您每月开始的库存单位。最后三个值为“4”,因为它们是上个月结转的剩余库存(来自安全库存)。
  • 没错,但是是在第1个月,所以前面2个月的库存也需要根据剩余库存计算,同时还要确保安全库存,正如另一个答案所指出的那样。跨度>

标签: r dplyr statistics


【解决方案1】:

假设这些库存是不易腐烂的,那么每个月的初始stock 应该等于您上个月剩余的数量。

您每个月的 to_purchase 方程应该是
to_purchase = safety_stock - (stock - consumption + arriving)
基本上是需求减去可用供应。

library(tidyverse)

df <- data.frame(
  type = c("a","a","a","a","a"),
  date = as.Date(c("2020-01-01", "2020-02-01", "2020-03-01", "2020-04-01", "2020-05-01")),
  stock = c(3,7,4,4,4),
  arriving = c(4,1,1,3,2),
  consumption = c(0,7,5,5,3),
  safety_stock = c(4,4,4,4,4)
) %>% 
  mutate(
    to_purchase = safety_stock - (stock - consumption + arriving), # purchase equation
    to_purchase = ifelse(to_purchase < 0, 0, to_purchase) # if negative, change value to zero
  )

但是,这样做的一个问题是,如果您要计算需要购买的数量 (to_purchase),您可能还需要计算上个月剩余的数量 (@987654326 @)。因此,您的代码还应包括每个月初始 stock 的计算。

df <- data.frame(
  type = c("a","a","a","a","a"),
  date = as.Date(c("2020-01-01", "2020-02-01", "2020-03-01", "2020-04-01", "2020-05-01")),
  stock = c(3,NA,NA,NA,NA),
  arriving = c(4,1,1,3,2),
  consumption = c(0,7,5,5,3),
  safety_stock = c(4,4,4,4,4),
  to_purchase = NA
) 

for(i in 2:nrow(df)){
  # determine if you need to purchase anything from the previous month
  h = i-1
  df$to_purchase[h] = df$safety_stock[h] - (df$stock[h] - df$consumption[h] + df$arriving[h])
  df$to_purchase[h] = ifelse(df$to_purchase[h] < 0, 0, df$to_purchase[h])
  # determine initial stock of the current month
  df$stock[i] = df$stock[h] + df$arriving[h] - df$consumption[h] + df$to_purchase[h]
  # determine if you need to purchase anything for the current month
  df$to_purchase[i] = df$safety_stock[i] - (df$stock[i] - df$consumption[i] + df$arriving[i])
  df$to_purchase[i] = ifelse(df$to_purchase[i] < 0, 0, df$to_purchase[i])
}

df
#>   type       date stock arriving consumption safety_stock to_purchase
#> 1    a 2020-01-01     3        4           0            4           0
#> 2    a 2020-02-01     7        1           7            4           3
#> 3    a 2020-03-01     4        1           5            4           4
#> 4    a 2020-04-01     4        3           5            4           2
#> 5    a 2020-05-01     4        2           3            4           1

【讨论】:

    猜你喜欢
    • 2018-10-13
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 2018-05-06
    相关资源
    最近更新 更多