title: Interpolate between two dynamic days
body: <p>We have a file that is updated every week and its structure is as follows:</p>
  
  <pre><code>      ID            Code    Days  Yield
  KZW1KM093436    NTK273_1343 4    2.1781
  KZW1KD913708    NTK091_1370 11   2.1683
  KZK1KY011222    MKM012_0122 17   2.1602
  KZW1KM063603    NTK182_1360 32   2.1419
  KZW1KM093477    NTK273_1347 32   2.1419
  KZW1KD913740    NTK091_1374 39   2.1342
  KZW1KM063629    NTK182_1362 46   2.1269
  KZW1KM093501    NTK273_1350 53   2.1202
  </code></pre>
  
  <p>I have the following static data points (Days) for which I need <code>Yields</code> on a weekly basis:</p>
  
  <pre><code>28
  91
  182
  273
  364
  730
  1825
  2555
  3640
  </code></pre>
  
  <p>When the file gets updated, the structure of the data also changes (sorted by <code>Days</code>). There are several possible scenarios:</p>
  
  <ul>
  <li>If the value in <code>Days</code> column of the data is equal to any of the
  values in my <code>Static</code> data, then I'd like to take the corresponding
  value from <code>Yield</code> column.</li>
  <li>Otherwise, I'd like to take the closest upper and lower values from
  <code>Days</code> and get interpolated <code>Yield</code> (eg. for my static <code>28</code> days in
  this particular case I'd have to use these two rows: <code>KZK1KY011222 MKM012_0122 17   2.1602</code> and <code>KZW1KM063603 NTK182_1360 32  2.1419</code>).</li>
  <li>If there are no beginning or ending values, which mostly applicable
  to <code>28</code> and <code>3640</code>, then I want to take the <code>Yield</code> from the closest
  <code>Days</code>.</li>
  </ul>
  
  <p>I was thinking about doing it in Excel/VBA, but would also like to see R solution. Any help would be greatly appreciated.</p>
  
  <p>Expected output for <code>Yield</code> is calculated according to this formula:</p>
  
  <p><a href="https://i.stack.imgur.com/lXw9o.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lXw9o.png" alt="enter image description here"></a></p>
  
  <p>So in this case for <code>28</code> Days I'd have <code>Yield</code> <code>(28 - 17)*(2.1419-2.1602)/(32-17)+2.1602 = 2.1468</code></p>