0

我有一个这样的数据集:

MQ = data.frame(Model=c("C150A","B174","DG18"),Quantity=c(5000,3800,4000))

MQ 是一个data.frame,它显示了未来一周的生产计划。带模型生产模型和数量

C150A = data.frame( Material=c("A0015", "A0071", "Z00071", "Z00080","Z00090", 
                   "Z00012","SZ0001"), Number=c(1,1,1,1,1,1,4))
B174= data.frame(Material=c("A0014","A0071","Z00080","Z00091","Z00011","SZ0000"), 
                                      Number=c(1,1,1,1,2,4))
DG18= data.frame( Material=c("A0014","A0075","Z00085","Z00090","Z00010","SZ0005"),
                                       Number=c(1,1,1,2,3,4))
T75A= data.frame(Material=c("A0013","A0075","Z00085","Z00090","Z00012","SZ0005"),
                                       Number=c(1,1,1,2,3,4))
G95= data.frame(Material=c("A0013","A0075","Z00085","Z00090","Z00017","SZ0008"),
                                       Number=c(1,1,1,2,3,4))

这些是可以生产的模型...

我的第一个问题是,属于 Productionplan MQ,我想自动打开所需的模型,并将数量乘以数字,以了解每个组件(材料)需要多少。

输出可以是一个data.frame,其中所有需要的组件(不同的模型可以使用相同的组件和不同的组件,所需的组件的数量也可以不同)在生产计划中注明的所有模型都被组合在一起。

 Material_Master= data.frame( Material=c( "A0013", "A001","A0015", "A0071", "A0075", 
                                "A0078", "Z00071", "Z00080", "Z00090", "Z00091", 
                                "Z00012","Z00091","Z00010""Z00012","Z00017","SZ0001", 
                                "SZ0005","SZ0005","SZ0000","SZ0008","SZ0009"), 
                              Number=c(20000,180000,250000,480000,250000,170000, 
                                       690000,1800000,17000,45000,12000,5000, 5000, 
                                       8000,16000,17000,45000,88000,7500,12000,45000))

在最后一步中,应将创建的 data.frame 与 Material_Master 数据合并:在 Material Master 数据中,所有重要的组件都已注明库存。

在我的示例中,Material Master 中还记录了生产所需的所有组件,但也可能是 Material_Master 中缺少一个组件,然后忽略此组件。

输出应该类似于,将所需的组件数量与它们的实际库存进行比较。给一个报告,如果有更多的需要,那么实际的库存有。

谢谢您的帮助。

4

1 回答 1

2

这应该有效:

mods <- do.call(rbind,lapply(MQ$Model,function(x)cbind(Model=x,get(x))))
full_plan <- merge(mods,MQ,by="Model")
material_plan <- with(full_plan,aggregate(Quantity*Number,by=list(Material),sum))
#    Group.1     x
# 1    A0014  7800
# 2    A0015  5000
# 3    A0071  8800
# 4    A0075  4000
# 5   SZ0000 15200
# 6   SZ0001 20000
# 7   SZ0005 16000
# 8   Z00010 12000
# 9   Z00011  7600
# 10  Z00012  5000
# 11  Z00071  5000
# 12  Z00080  8800
# 13  Z00085  4000
# 14  Z00090 13000
# 15  Z00091  3800

第一行获取每个模型并将它们与模型名称一起堆叠。第二行合并回来得到数量,第三行聚合。

我继续并通过在后面的模型中修剪1每个开头的 来制作一个可用的示例。Number此外,我将模型和材料列作为字符而不是因子读取。

options(stringsAsFactors=FALSE)
MQ = data.frame(Model=c("C150A","B174","DG18"),Quantity=c(5000,3800,4000))

C150A = data.frame(Material=c("A0015","A0071","Z00071","Z00080","Z00090","Z00012","SZ0001"),Number=c(1,1,1,1,1,1,4))
B174= data.frame(Material=c("A0014","A0071","Z00080","Z00091","Z00011","SZ0000"), Number=c(1,1,1,1,2,4))
DG18= data.frame(Material=c("A0014","A0075","Z00085","Z00090","Z00010","SZ0005"),Number=c(1,1,1,2,3,4))
T75A= data.frame(Material=c("A0013","A0075","Z00085","Z00090","Z00012","SZ0005"),Number=c(1,1,1,2,3,4))
G95= data.frame(Material=c("A0013","A0075","Z00085","Z00090","Z00017","SZ0008"),Number=c(1,1,1,2,3,4))

编辑:添加了所需的 stringsAsFactors 选项,由@RicardoSaporta 标识。

于 2013-05-14T02:18:00.423 回答