title: How to count matches between a vector and dataframe of sequence coordinates?
body: <p>Given a data table with start and end coordinates for sequences of integers:</p>
  
  <pre><code>set.seed(1)
  
  df1 &lt;- data.table(
    START = c(seq(1, 10000000, 10), seq(1, 10000000, 10), seq(1, 10000000, 10)),
    END = c(seq(10, 10000000, 10), seq(10, 10000000, 10), seq(10, 10000000, 10)) 
  </code></pre>
  
  <p>And a vector of integers: </p>
  
  <pre><code>vec1 &lt;- sample(1:100000, 10000)
  </code></pre>
  
  <p>How can I count the number of integers in vec1 that are within the start and end coordinates of each sequence in df1? I am currently using a for loop: </p>
  
  <pre><code>COUNT &lt;- rep(NA, nrow(df1)) 
  for (i in 1:nrow(df1)){
    vec2 &lt;- seq(from = df1$START[i], to = df1$END[i])
    COUNT[i] &lt;- table(vec2 %in% vec1)[2]
    print(i)
  }
  df1$COUNT &lt;- COUNT
  </code></pre>
  
  <p>However, the datatable and vector I am applying this to are very large? Is anyone able to suggest a way to improve performance? </p>
  
  <p>Any help will be greatly appreciated! </p>