title: Find x-intercept and y-intercept of a loop in R
body: <p>How can I find x-intercept and the y-intercept (all four) of a loop with <code>R</code>?</p>
  
  <pre><code># this will generate an example dataframe mydf
  x &lt;- sin(seq(0,2*pi,0.2) + rnorm(1))
  y &lt;- cos(seq(0,2*pi,0.2) + rnorm(1))
  
  mydf &lt;- data.frame(x,y)
  plot(mydf)
  abline(h=0)
  abline(v=0)
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/pjC2B.png" alt="impression of the loop"></p>
  
  <p>At the moment I try around with two similar functions, that divide in north/south/east/west
  and look for the closest point to the axis. This is inefficient and not precise, because the point might be far away.</p>
  
  <pre><code>getYintercept &lt;- function(mdf){
    R1 &lt;- mdf$y[which.min(subset(mdf, y&gt;0)$y)] # north
    R2 &lt;- mdf$y[which.min(subset(mdf, y&lt;0)$y)] # south
    return(rbind(R1,R2))
  }
  </code></pre>
  
  <p>Thus I want to interpolate (<code>predict</code>, <code>approx</code>) this point. 
  How can this be solved most elegant? </p>