title: Linear interpolation in R
body: <p>I have a dataset of real data, for example looking like this:</p>
  
  <pre><code># Dataset 1 with known data
  known &lt;- data.frame(
      x = c(0:6),
      y = c(0, 10, 20, 23, 41, 39, 61)
  )
  
  plot (known$x, known$y, type="o")
  </code></pre>
  
  <p>Now I want to get an aswer to the question
  "What would the Y value for 0.3 be, if all intermediate datapoints of the original dataset, are on a straight line between the surrounding measured values?"</p>
  
  <pre><code> # X values of points to interpolate from known data
   aim &lt;- c(0.3, 0.7, 2.3, 3.3, 4.3, 5.6, 5.9)
  </code></pre>
  
  <p>If you look at the graph: I want to get the Y-Values, where the ablines intersect with the linear interpolation of the known data</p>
  
  <pre><code>abline(v = aim, col = "#ff0000")
  </code></pre>
  
  <p>So, in the ideal case I would create a "linearInterpolationModel" with my known data, e.g.</p>
  
  <pre><code>model &lt;- linearInterpol(known)
  </code></pre>
  
  <p>... which I can then ask for the Y values, e.g.</p>
  
  <pre><code>model$getEstimation(0.3)
  </code></pre>
  
  <p>(which should in this case give "3")</p>
  
  <pre><code>abline(h = 3, col = "#00ff00")
  </code></pre>
  
  <p>How can I realize this? Manually I would for each value do something like this:</p>
  
  <ol>
  <li>What is the closest X-value smaller <code>Xsmall</code> and the closest X-value larger <code>Xlarge</code> than the current X-value <code>X</code>.</li>
  <li>Calculate the relative position to the smaller X-Value <code>relPos = (X - Xsmall) / (Xlarge - Xsmall)</code></li>
  <li>Calculate the expected Y-value <code>Yexp = Ysmall + (relPos * (Ylarge - Ysmall))</code></li>
  </ol>
  
  <p>At least for the software Matlab I heard that there is a built-in function for such problems.</p>
  
  <p>Thanks for your help,</p>
  
  <p>Sven</p>