减少库存循环

时间:2018-09-28 21:58:29

标签: r

input data.frame picture

output data

我有一个订单数据框,我想每天减少库存。我开始编写一个循环以减少每种装运类型的库存。对于订单的每一天,从该天起的订单需要离开的连续6天(在Leave_day列中)。我想做的是编写一个以units_to_ship与ship_capacity开始的循环(示例-11/20当天有132393个新单位,但是船的容量为130000)。 Leave_day 1-5可以正常工作,但是在Leave_day 6的13K中只有大约10,000可以发货。要编写一个循环,以在处理Leave_day之后减少每个order_dt中的库存。 希望输出看起来像“测试”数据框,但也带有units_remaining列(例如:不发货的〜3k)。

这是我的代码:

order_dt <- c("43424", "43425", "43426", "43427", "43428", "43429", "43430",
           "43431", "43432", "43433", "43434", "43435", "43436", "43437",
           "43438", "43439", "43440", "43441")
new_units_proj <- c(132993, 408989, 584165, 710695, 654237, 353607, 
                    469608, 433387, 93930, 143648, 167652, 164357, 
                    162038, 151675, 180338, 186996, 172749, 150275)
ship_capacity <- c(130000, 160000, 300000, 340000, 375000, 375000, 375000, 
                   375000, 375000, 375000, 375000, 375000, 375000, 325000, 
                   160000, 170000, 168000, 173000)
inputs <- cbind(order_dt, new_units_proj, ship_capacity)
inputs <- data.table(inputs)
inputs <- inputs %>% 
  .[, 
    `:=` (order_dt = as.numeric(order_dt), 
          new_units_proj = as.numeric(new_units_proj), 
          ship_capacity = as.numeric(ship_capacity))] %>% 
  .[, order_dt := as.Date(inputs$order_dt, origin = '1899-12-30')]

leave_day <- data.frame(leave_day = 1:6, 
                        pct_out = c(.15, .33, .16, .11, .15, .1))
leave_day <- data.table(leave_day)

cross.join <- function(a, b) {
  idx <- expand.grid(seq(length=nrow(a)), seq(length=nrow(b)))
  cbind(a[idx[,1],], b[idx[,2],])
}

orders <- cross.join(inputs, leave_day)
orders <- orders %>% arrange(order_dt)
orders <- orders %>% 
  mutate(units_to_ship = round(pct_out * new_units_proj))
test <- orders %>% 
  filter(order_dt == as.Date('2018-11-20')) %>% 
  mutate(remaining_capacity = NA, new_capacity = NA) %>% 
  select(order_dt, new_units_proj, leave_day, pct_out, units_to_ship, 
         ship_capacity, remaining_capacity, new_capacity) %>% 
  mutate(remaining_capacity = ship_capacity - units_to_ship) %>%
  mutate(new_capacity = lag(remaining_capacity, n = 1) - units_to_ship)


test <- orders %>% 
  filter(order_dt == as.Date('2018-11-20'))

for(i in 1:nrow(test)){ 

  test$new_cap <- test$ship_capacity - test$units_to_ship
}

#suppressWarnings(
for (i in 1:nrow(test)){

  test$remaining_capacity = test$ship_capacity - test$units_to_ship

  if(i == nrow(test)){
    test$ship_capacity[i] <- test$remaining_capacity[i]
  } else
    test$ship_capacity[i + 1] <- test$remaining_capacity[i]

  if(test$units_to_ship <= test$ship_capacity){
    test$shipped_actual <- test$units_to_ship
  } else
    test$leftover_units <- test$units_to_ship - test$remaining_capacity

}
#)

0 个答案:

没有答案