title: Fill the NAs values in a dataframe based on calculation of columns in other dataframe
body: <p>I have 2 dataframes. The <code>RP</code> and the <code>RP2</code>. My goal is to fill the <code>NAs</code> of <code>RP</code> columns <code>Country_1</code> and <code>Country_2</code> based on the conditions below:</p>
  <p>1.If a cell of <code>RP</code> has already value leave it as it is.</p>
  <p>2.If it has <code>NA</code> then see the <code>TreatmenArea</code> and <code>Subarea</code> of that cell and find the same pair of <code>TreatmenArea</code>, <code>Subarea</code> in <code>RP2</code> then:</p>
  <p>a.multiply this value with the <code>PFaverage</code> column of <code>RP2</code> and divide by 100.</p>
  <p>b.if the relative cell in <code>RP2</code> is <code>NA</code> as well then leave the value in <code>RP</code> as <code>NA</code></p>
  <pre><code>RP&lt;-structure(list(ProductFamily = c(&quot;PF_1&quot;, &quot;PF_10&quot;, &quot;PF_10&quot;, &quot;PF_100&quot;, 
  &quot;PF_100&quot;, &quot;PF_101&quot;, &quot;PF_102&quot;, &quot;PF_102&quot;, &quot;PF_102&quot;, &quot;PF_102&quot;, &quot;PF_103&quot;, 
  &quot;PF_103&quot;, &quot;PF_104&quot;, &quot;PF_105&quot;, &quot;PF_106&quot;, &quot;PF_106&quot;, &quot;PF_106&quot;, &quot;PF_106&quot;, 
  &quot;PF_107&quot;, &quot;PF_108&quot;, &quot;PF_109&quot;, &quot;PF_11&quot;, &quot;PF_110&quot;, &quot;PF_110&quot;, &quot;PF_111&quot;, 
  &quot;PF_111&quot;, &quot;PF_111&quot;, &quot;PF_112&quot;, &quot;PF_112&quot;, &quot;PF_113&quot;, &quot;PF_113&quot;, &quot;PF_113&quot;, 
  &quot;PF_114&quot;, &quot;PF_115&quot;, &quot;PF_115&quot;, &quot;PF_116&quot;, &quot;PF_117&quot;, &quot;PF_118&quot;, &quot;PF_119&quot;, 
  &quot;PF_12&quot;, &quot;PF_12&quot;, &quot;PF_12&quot;, &quot;PF_120&quot;, &quot;PF_120&quot;, &quot;PF_120&quot;, &quot;PF_120&quot;, 
  &quot;PF_120&quot;, &quot;PF_120&quot;, &quot;PF_121&quot;, &quot;PF_122&quot;), TreatmenArea = c(&quot;TA_7&quot;, 
  &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_6&quot;, &quot;TA_6&quot;, &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_2&quot;, 
  &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_2&quot;, 
  &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_7&quot;, &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_7&quot;, 
  &quot;TA_7&quot;, &quot;TA_7&quot;, &quot;TA_7&quot;, &quot;TA_7&quot;, &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_6&quot;, 
  &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_5&quot;, &quot;TA_5&quot;, 
  &quot;TA_5&quot;, &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_4&quot;, 
  &quot;TA_4&quot;), Subarea = c(&quot;SA_16&quot;, &quot;SA_5&quot;, &quot;SA_5&quot;, &quot;SA_15&quot;, &quot;SA_15&quot;, 
  &quot;SA_6&quot;, &quot;SA_6&quot;, &quot;SA_6&quot;, &quot;SA_6&quot;, &quot;SA_6&quot;, &quot;SA_6&quot;, &quot;SA_6&quot;, &quot;SA_6&quot;, 
  &quot;SA_6&quot;, &quot;SA_6&quot;, &quot;SA_6&quot;, &quot;SA_6&quot;, &quot;SA_6&quot;, &quot;SA_6&quot;, &quot;SA_6&quot;, &quot;SA_6&quot;, 
  &quot;SA_17&quot;, &quot;SA_6&quot;, &quot;SA_6&quot;, &quot;SA_22&quot;, &quot;SA_22&quot;, &quot;SA_22&quot;, &quot;SA_22&quot;, 
  &quot;SA_22&quot;, &quot;SA_6&quot;, &quot;SA_6&quot;, &quot;SA_6&quot;, &quot;SA_15&quot;, &quot;SA_6&quot;, &quot;SA_6&quot;, &quot;SA_6&quot;, 
  &quot;SA_6&quot;, &quot;SA_6&quot;, &quot;SA_6&quot;, &quot;SA_11&quot;, &quot;SA_11&quot;, &quot;SA_11&quot;, &quot;SA_6&quot;, &quot;SA_6&quot;, 
  &quot;SA_6&quot;, &quot;SA_6&quot;, &quot;SA_6&quot;, &quot;SA_6&quot;, &quot;SA_10&quot;, &quot;SA_10&quot;), Country_1 = c(NA, 
  NA, NA, NA, NA, 92.6961421759861, NA, NA, NA, NA, 78.3001808318264,NA, NA, NA, 106.832963501416, 0.613496932515337, 104.21011973735, 
  NA, NA, NA, NA, NA, 99.5238622522423, NA, NA, NA, NA, NA, NA, 
  89.0343347639485, NA, NA, NA, NA, NA, NA, 101.231684009344, NA, 
  NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), Country_2 = c(NA, 
  NA, 161.55950752394, NA, NA, NA, NA, NA, 59.1346153846154, NA, 
  NA, NA, NA, 128.113063407181, 93.3812839543959, NA, NA, NA, 137.724550898204, 
  NA, NA, NA, NA, 90.1602849510241, 37.3939722071828, NA, NA, 40.9756097560976, 
  NA, NA, NA, 87.0095902353967, NA, NA, NA, 50.4591590140164, 92.1639413888299, 
  44.7601588756493, NA, NA, NA, NA, NA, 100.053835800808, NA, NA, 
  NA, NA, 136.420722135008, NA)), row.names = c(NA, 50L), class = &quot;data.frame&quot;)
  </code></pre>
  <p>and the RP2</p>
  <pre><code>RP2&lt;-structure(list(TreatmenArea = c(&quot;TA_1&quot;, &quot;TA_1&quot;, &quot;TA_1&quot;, &quot;TA_1&quot;, 
  &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_2&quot;, &quot;TA_3&quot;, &quot;TA_4&quot;, &quot;TA_4&quot;, &quot;TA_5&quot;, &quot;TA_5&quot;, 
  &quot;TA_5&quot;, &quot;TA_6&quot;, &quot;TA_6&quot;, &quot;TA_7&quot;, &quot;TA_7&quot;, &quot;TA_7&quot;, &quot;TA_7&quot;, &quot;TA_7&quot;, 
  &quot;TA_7&quot;, &quot;TA_7&quot;, &quot;TA_7&quot;, &quot;TA_8&quot;, &quot;TA_9&quot;), Subarea = c(&quot;SA_1&quot;, 
  &quot;SA_2&quot;, &quot;SA_3&quot;, &quot;SA_4&quot;, &quot;SA_5&quot;, &quot;SA_6&quot;, &quot;SA_7&quot;, &quot;SA_8&quot;, &quot;SA_10&quot;, 
  &quot;SA_9&quot;, &quot;SA_11&quot;, &quot;SA_12&quot;, &quot;SA_13&quot;, &quot;SA_14&quot;, &quot;SA_15&quot;, &quot;SA_16&quot;, 
  &quot;SA_17&quot;, &quot;SA_18&quot;, &quot;SA_19&quot;, &quot;SA_20&quot;, &quot;SA_21&quot;, &quot;SA_22&quot;, &quot;SA_23&quot;, 
  &quot;SA_24&quot;, &quot;SA_25&quot;), Country_1 = c(101.37519256645, 105.268942332558, 
  100.49933368058, 104.531597221684, NaN, 83.4404308144341, 86.2833044714836, 
  81.808967345926, 79.6786979951661, 77.6863475527052, NaN, 78.3001808318264, 
  112.499238782021, 113.526674294436, NaN, 108.350959378962, NaN, 
  NaN, 102.243471199266, NaN, 104.323270355678, NaN, NaN, NaN, 
  100), Country_2 = c(98.7267717862572, 83.9572019653478, 97.164068306148, 
  103.654771613923, 161.55950752394, 75.4091957339533, 96.5255996196344, 
  99.8317785594128, 88.1477193135348, NaN, NaN, NaN, 151.411687458963, 
  107.652477161141, NaN, NaN, NaN, NaN, 92.4695215620261, NaN, 
  88.9890571623243, 39.1847909816402, 87.0642912470953, NaN, NaN
  ), PFaverage = c(9.09293100169062, 16.2821052631579, 9.76688333333333, 
  13.4754047619048, 17.8741666666667, 9.42546567085954, 12.6145188492064, 
  12.4536666666667, 7.36169471153846, 13.1581818181818, 21.0866666666667, 
  9.58, 16.2525049019608, 11.552822039072, 12.6908333333333, 20.5489611111111, 
  11.99, 7.19134920634921, 8.51728472222222, 5.97, 12.2264183501684,6.37925, 16.1375, 11.9384615384615, 15.9185714285714)), class = c(&quot;data.frame&quot;), row.names = c(NA, -25L)) 
  </code></pre>
  <p>If for example we check first row of <code>Country_1</code> in RP we see that its <code>NA</code> and its <code>Subarea</code> is <code>SA_16</code> so we move to <code>RP2</code> and check that the relative value for <code>Country_1</code> and <code>Subarea SA_16</code> is <code>108.35</code>. So the calculation is <code>108.35*20.54(PFaverage)/100</code>.</p>