2017-05-27 58 views
0

我試圖計算手頭庫存持續的週數,給出數據集的銷售預測,其中包含數百萬行的數據集。我在下面給出的數據結構的最後一列中列出了預期產出。我也在Excel中附加了這個實現。 (在這裏,周顯示在列,但它應該像顯示在預期輸出行:在最後一列 R - 供應週數

Data = structure(list(
SKU_CD = c(111, 111, 111, 111, 111, 111, 111,111, 111, 111, 111, 111, 222, 222, 222, 222, 222, 222, 222, 222, 222, 222, 222, 222), 
STORE_CD = c(22, 22, 22, 22, 22, 22, 22,22, 22, 22, 22, 22, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33), 
FWK_CD = c(201627, 201628, 201629, 201630, 201631, 201632,201633, 201634, 201635, 201636, 201637, 201638, 201627, 201628, 201629, 201630, 201631, 201632, 201633, 201634, 201635, 201636, 201637, 201638), 
SALES = c(5, 2, 2, 2, 1, 3, 2, 2, 3, 2, 3, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 7, 5), 
INVENTORY = c(29, 27, 25, 23, 22, 19, 17, 15, 12, 10, 25, 1, 19, 17, 15, 13, 12,9, 7, 5, 2, 0, 25, 18), 
WeeksofSupply = c("11", "10", "9", "8", "8", "6", "5", "4", "3", "2", "Inventory More", "Inventory Less", "2", "2", "1", "1", "1", "Inventory Less", "Inventory Less", "Inventory Less", "Inventory Less", "Inventory Less", "Inventory More", "Inventory More")), 
class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -24L), 
.Names = c("SKU_CD", "STORE_CD", "FWK_CD", "SALES", "INVENTORY", "WeeksofSupply")) 

Expected output in the last column

當前的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. 

預期輸出。 )

=IF(A2<SUM(B2:K2),SUMPRODUCT(--(SUBTOTAL(9,OFFSET(B2:K2,,,,COLUMN(B2:K2)- 
COLUMN(B2)+1))<=A2))+LOOKUP(0,SUBTOTAL(9,OFFSET(B2:K2,,,,COLUMN(B2:K2)- 
COLUMN(B2)+1))-B2:K2-A2,(A2-(SUBTOTAL(9,OFFSET(B2:K2,,,,COLUMN(B2:K2)- 
COLUMN(B2)+1))-B2:K2))/B2:K2),IF(A2=SUM(B2:K2),COUNT(B2:K2),"Inventory 
exceeds forecast")) 

Excel Example

我將不勝感激任何投入,以在R中有效實施此許多感謝您的時間!

回答

0

對於長格式的修正後的數據,你可以做以下...

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 
+0

非常感謝!這非常有幫助。 – S5164456135

1

這裏是一個辦法做到這一點,採用線性插值法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 
+0

謝謝。任何有關如何實現這一點與周作爲一個列而不是多個的想法。非常感謝您的意見! – S5164456135

+0

我已經發布它作爲第二個答案。 –