合并不同Vector的同一行并在之后进行多重化

时间:2013-05-14 01:43:06

标签: r merge assign

我有这样的数据集:

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

MQ是一个data.frame,它显示了未来一周的Productionplan。用模型生成模型和数量

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,其中所有需要的组件(不同的模型可以使用相同的组件和不同的组件,也可以使用所需组件的数量不同),而不是生产计划中的所有组件。< / p>

 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中缺少组件,然后忽略此组件。

输出应该是这样的,比较所需的组件数量,以及它们的实际库存。报告,如果有更多需要,那么实际库存有。

感谢您的帮助。

1 个答案:

答案 0 :(得分: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来提供一个可用的示例。另外,我以字符而不是因子的形式读取了Model和Material列。

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标识。