title: Conditional mathematical optimization in R
body: <p>I have the following data frames:</p>
  
  <pre><code>Required &lt;- data.table( Country=c("AT Iron", "AT Energy", "BE Iron", "BE Energy", "BG Iron", "BG Energy"),Prod1=c(5,10,0,5,0,5),Prod2=c(25,5,10,0,0,5))
  Supplied &lt;- data.table( Country=c("AT Iron", "AT Energy", "BE Iron", "BE Energy", "BG Iron", "BG Energy"),Prod1=c(10,5,5,10,5,10),Prod2=c(20,20,20,0,15,10))
  
  
  &gt; Required
       Country Prod1 Prod2
  1:   AT Iron     5    25
  2: AT Energy    10     5
  3:   BE Iron     0    10
  4: BE Energy     5     0
  5:   BG Iron     0     0
  6: BG Energy     5     5
  
  &gt; Supplied
       Country Prod1 Prod2
  1:   AT Iron    10    20
  2: AT Energy     5    20
  3:   BE Iron     5    20
  4: BE Energy    10     0
  5:   BG Iron     5    15
  6: BG Energy    10    10
  </code></pre>
  
  <p>"Required" shows the initial material and energy requirements to manufacture two products, and the materials and energy are supplied by three different countries. For example, product 1 would require, for Energy, 10 units from AT, 5 units from BE and 5 units from BG. "Supplied" shows the actual supply capacity of the countries. Following the example, AT cannot supply 10 units of energy but 5 units, so another country must supply the remaining units. I assume that the country with the most net supply capacity (that is, once discounted the initial requirement) will provide the remaining units. In this case, both BE and BG have 5 units of net supply capacity, so both will provide with equal units, 2.5.</p>
  
  <p>I seek an optimization algorithm that creates a new "Required" table, "RequiredNew", considering supply constrains and the above mentioned assumption. The resulting table should look like:</p>
  
  <pre><code>&gt; RequiredNew
       Country Prod1 Prod2
  1:   AT Iron     5    20
  2: AT Energy    10     5
  3:   BE Iron     0    10
  4: BE Energy   7.5     0
  5:   BG Iron     0     5
  6: BG Energy   7.5     5
  </code></pre>
  
  <p>In the link below I posted a similar question which was solved by user digEmAll, so a similar approach would be suitable. However, I rephrased the question so that it becomes clearer and resembles more to my actual data.</p>
  
  <p><a href="https://stackoverflow.com/questions/31923107/mathematical-optimization-in-r/31925479#31925479">Mathematical optimization in R</a></p>
  
  <p>I apologise by the multiple posts. Thank you in advance.</p>