R 铁路车辆分布

R 铁路车辆分布,r,dplyr,statistics,R,Dplyr,Statistics,我有一个数据框,包含每月的库存、到货数量和预期消费 我需要计算我每个月需要购买的数量,以保持我的股票高于每月一定的安全库存 例如:我有3台库存,4台在第一个月到达,预计消耗量为0。因此,我本月不需要购买任何单位,剩下的是3+4-0=7 这意味着下个月我将从7开始,假设消耗量为7,到达1个单位。因此,我将低于我的安全库存4,所以我需要计划购买3个单位,该月 第三个月,我从4个单位的安全库存开始,1个单位到达,消耗量5,所以我是0,所以我需要计划购买4个单位来补充我的安全库存 作为一个例子,我需要复

我有一个数据框,包含每月的库存、到货数量和预期消费

我需要计算我每个月需要购买的数量,以保持我的股票高于每月一定的安全库存

例如:我有3台库存,4台在第一个月到达,预计消耗量为0。因此,我本月不需要购买任何单位,剩下的是3+4-0=7

这意味着下个月我将从7开始,假设消耗量为7,到达1个单位。因此,我将低于我的安全库存4,所以我需要计划购买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))

df假设这些库存是不易腐烂的,那么每个月的初始
库存
应等于您上个月剩下的金额

您每个月的
to_purchase
等式应该是
to_purchase=安全库存-(库存-消费+到达)

基本上是需求减去可用供应

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
  )

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