我有一个包含一定数量不同文章的商店列表和一个包含这些文章的仓库 - 这是两个独立的数据框。
Article <- c('a','b','a','b','c','d')
forecast <- c( 1,5,80,10,100,1000)
StoreID <- c(1,1,2,2,3,4)
StoreData <- data.frame(StoreID, Article, Order)
像这样的Smth:
StoreData
StoreID Article forecast
1 a 1
1 b 5
2 a 80
2 b 10
3 c 100
4 d 1000
仓库数据:
Stock <- c(10,11,12,100)
WarehouseData <- data.frame(Article, Stock)
WarehouseData
Article Stock
a 10
b 11
c 12
d 100
我的目标是拥有采购订单栏。逻辑必须遵循:对于StoreData表中的每一行,我必须查看仓库中物品的库存以及是否足够 - 批准fcst,如果不是 - 仅批准可用数量。我的问题是,在批准数量时,可用库存正在减少,我无法了解如何将其考虑在内。
预期结果如下:
StoreData
StoreID Article forecast PO
1 a 1 1
1 b 5 5
2 a 80 9
2 b 10 6
3 c 100 12
4 d 1000 100
请有人,请告诉我们如何做对吗?
答案 0 :(得分:3)
这是使用dplyr
的另一种方法:
library(dplyr)
left_join(storeData, WarehouseData, by = "Article") %>%
group_by(Article) %>%
mutate(PO = ifelse(cumsum(forecast) <= Stock, forecast,
Stock - cumsum(forecast) + forecast)) %>% ungroup
#Source: local data frame [6 x 5]
#
# StoreID Article forecast Stock PO
# (int) (fctr) (int) (dbl) (dbl)
#1 1 a 1 10 1
#2 1 b 5 11 5
#3 2 a 80 10 9
#4 2 b 10 11 6
#5 3 c 100 12 12
#6 4 d 1000 100 100
答案 1 :(得分:1)
请参阅下面的循环,例如:
StoreData$PO <- NA
for (i in 1:nrow(StoreData)) {
query <- WarehouseData$Article == StoreData[i, "Article"]
po <- ifelse(StoreData[i, "forecast"] > WarehouseData[query, 2],
WarehouseData[query, 2],
StoreData[i, "forecast"])
WarehouseData[query, 2] <- WarehouseData[query, 2] - po
StoreData[i, "PO"] <- po
}
print(StoreData)
# StoreID Article forecast PO
# 1 1 a 1 1
# 2 1 b 5 5
# 3 2 a 80 9
# 4 2 b 10 6
# 5 3 c 100 12
# 6 4 d 1000 100
这是另一种基于使用基础R的其他解决方案的替代方案:
StoreData <- merge(StoreData, WarehouseData)
StoreData$PO <- do.call(c, lapply(split(StoreData, StoreData$Article), function(z) {
ifelse(cumsum(z$forecast) <= z$Stock, z$forecast,
z$Stock - cumsum(z$forecast) + z$forecast)
}))
以下是我用来重新创建数据集的内容,可能有助于其他答案:
StoreData <- read.table(text = "StoreID Article forecast
1 a 1
1 b 5
2 a 80
2 b 10
3 c 100
4 d 1000", header = T)
Article <- c('a','b','c','d')
Stock <- c(10,11,12,100)
WarehouseData <- data.frame(Article, Stock)