title: Find overlapping intervals
body: <p><strong>Context:</strong> As far as I can see, R lacks consistent functions which facilitate the data preparation in the context of survival/event history analysis, e.g. episode-splitting to include time-varying covariates (sometimes refered to as 'counting process data'). </p>
  
  <p>For each individual (<code>id</code>), the start (<code>start.cp</code>) and end time (<code>stop.cp</code>) of each episode is given. Furthermore, for each of the <em>1,2, ..., p</em> time-varying covariates (TVC), we know  when the episode starts (<code>tvc.start_</code>) and when it ends (<code>tvc.stop_</code>). </p>
  
  <p>In my example (see below) the number of TVCs is 2 but usually the number can vary (from 1 to p). </p>
  
  <p><strong>Example:</strong> </p>
  
  <p><em>Input data:</em></p>
  
  <pre><code>  id start.cp stop.cp tvc.start1 tvc.start2 tvc.stop1 tvc.stop2
  1  1        1       2          2          3         4         7
  2  1        2       3          2          3         4         7
  3  1        3       4          2          3         4         7
  4  1        4       7          2          3         4         7
  5  1        7      12          2          3         4         7
  
  structure(list(id = c(1, 1, 1, 1, 1), start.cp = c(1, 2, 3, 4, 
  7), stop.cp = c(2, 3, 4, 7, 12), tvc.start1 = c(2, 2, 2, 2, 2
  ), tvc.start2 = c(3, 3, 3, 3, 3), tvc.stop1 = c(4, 4, 4, 4, 4
  ), tvc.stop2 = c(7, 7, 7, 7, 7)), .Names = c("id", "start.cp", 
  "stop.cp", "tvc.start1", "tvc.start2", "tvc.stop1", "tvc.stop2"), 
  row.names = c(NA, 5L), class = "data.frame")
  </code></pre>
  
  <p>The names of the TVCs are known, i.e. in this example it is known that </p>
  
  <pre><code>tvc.start &lt;- c("tvc.start1", "tvc.start2") 
  tvc.stop &lt;- c("tvc.stop1", "tvc.stop2")
  </code></pre>
  
  <p><em>Expected results:</em></p>
  
  <pre><code>  id start.cp stop.cp tvc.start1 tvc.start2 tvc.stop1 tvc.stop2 tvc.d1 tvc.d2
  1  1        1       2          2          3         4         7      0      0
  2  1        2       3          2          3         4         7      1      0
  3  1        3       4          2          3         4         7      1      0
  4  1        4       7          2          3         4         7      0      1
  5  1        7      12          2          3         4         7      0      1
  
  structure(list(id = c(1, 1, 1, 1, 1), start.cp = c(1, 2, 3, 4, 
  7), stop.cp = c(2, 3, 4, 7, 12), tvc.start1 = c(2, 2, 2, 2, 2
  ), tvc.start2 = c(3, 3, 3, 3, 3), tvc.stop1 = c(4, 4, 4, 4, 4
  ), tvc.stop2 = c(7, 7, 7, 7, 7), tvc.d1 = c(0, 1, 1, 0, 0), tvc.d2 = c(0, 
  0, 0, 1, 1)), .Names = c("id", "start.cp", "stop.cp", "tvc.start1", 
  "tvc.start2", "tvc.stop1", "tvc.stop2", "tvc.d1", "tvc.d2"), row.names = c(NA, 
  5L), class = "data.frame")
  </code></pre>
  
  <p><strong>Question:</strong> For each TVC, I would like to create a new vector (<code>tvc.d1</code>, <code>tvc.d2</code>, see example) which indicates that a given episode (defined by <code>start.cp</code> and <code>stop.cp</code>) overlaps (=1) the interval of a TVC. It is assumed that <em>[start.cp, stop.cp)</em>. How can this be done without looping over the set of TVCs, i.e. I am looking for a vectorized solution. </p>
  
  <p><strong>P.S.:</strong> Please feel free to change the title...</p>