Вот решение, которое я нашел.
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