我有一个数据集,我需要从具有变化的连接性关系(一到一个接收点(RP)和交付点(DP)分配值,一对几,一到一对多,多对多TO-一个,多到少,多到多)。这些收据和交货值需要通过一个合同号进行分配,并接收和投递点之间的每个合同价值总是同样总结(即receiptsums == deliverysums),但个别的收货和交货值可能不是(即不是斌包装的问题)。一对一的关系很容易被过滤,其中合同号的接收值和交货值之和仅具有每个的一行解决(两行合计/合同)。
多到少(反之亦然)被证明是有问题的。
不要紧,从RP的DP接收其分配。
对于这个问题的解决方案有所解决手头的问题,就是我工作过R: Distributing an amount as evenly as possible II的
我开始上周六学习R代表该工作项目FWIW。在Excel中,我会做上ASC排名min函数,但我需要建立在某些情况下,新行,同时保持RP&DP的名称和合同号并不在这里工作。
我已经包括下面的图像。
任何指针将是有益的。
谢谢你,卡尔
量数据表
这里是我找到了解决办法。
databuild <-function() {
allonew <<- data.frame(Contract.Number=character(), Contract.Receipt.Point=character(),Contract.Delivery.Point=character(),Contract.Volumes.Mdthd=numeric())
allorow <- data.frame(Contract.Number=character(), Contract.Receipt.Point=character(),Contract.Delivery.Point=character(),Contract.Volumes.Mdthd=numeric())
deliveryct <<- 1
receipt.vols <- numeric()
receipt.vols <- c(200,310,100,40)
receipt.name <<- c("rtest1", "rtest2", "rtest3", "rtest4")
delivery.name <<- c("dtest1","dtest2")
contract <<- 1260
r.count <-length(receipt.vols)
contract.receipt.vols <<- receipt.vols[!is.na(receipt.vols)]
rec.sum <<- sum(receipt.vols)
contract.vols.sum <-rec.sum*2
delivery.vols <- c(500,150)
d.count <-length(delivery.vols)
contract.delivery.vols <<- delivery.vols[!is.na(delivery.vols)]
receipt.vols <- receipt.vols[receiptct]
delivery.vols <- delivery.vols[deliveryct]
browser()
while(contract.vols.sum/rec.sum !=1) {
if(receipt.vols == delivery.vols) {
while(receipt.vols == delivery.vols) {
contract.vols <-min(receipt.vols,delivery.vols)
contract.vols.sum <- contract.vols.sum - contract.vols
allorow <- as.data.frame(t(c(contract[1],receipt.name[receiptct],delivery.name[deliveryct],contract.vols)))
colnames(allorow) <- colnames(allonew)
allonew <-rbind(allonew,allorow)
deliveryct <-deliveryct + 1
receiptct <- receiptct + 1
if(receiptct > r.count) {
break
}
delivery.vols <- contract.delivery.vols[deliveryct]
receipt.vols <- contract.receipt.vols[receiptct]
}
} else if(receipt.vols < delivery.vols) {
while(receipt.vols < delivery.vols) {
contract.vols <- min(receipt.vols,delivery.vols)
delivery.vols <- delivery.vols - contract.vols
contract.vols.sum <- contract.vols.sum - contract.vols
allorow <- as.data.frame(t(c(contract[1],receipt.name[receiptct],delivery.name[deliveryct],contract.vols)))
colnames(allorow) <- colnames(allonew)
allonew <-rbind(allonew,allorow)
receiptct <- receiptct + 1
receipt.vols <- contract.receipt.vols[receiptct]
}
} else if(receipt.vols > delivery.vols) {
while(receipt.vols > delivery.vols) {
contract.vols <-min(receipt.vols,delivery.vols)
receipt.vols <- receipt.vols- contract.vols
contract.vols.sum <- contract.vols.sum - contract.vols
allorow <- as.data.frame(t(c(contract[1],receipt.name[receiptct],delivery.name[deliveryct],contract.vols)),stringsAsFactors = FALSE)
colnames(allorow) <- colnames(allonew)
allonew <-rbind(allonew,allorow)
deliveryct <- deliveryct + 1
delivery.vols <- contract.delivery.vols[deliveryct]
}
} else next
}
allonew <<- allonew