title: R comparison of vector to each row of data table is slow
body: <p>I need to compare a range of values (value_range) to values that are in rows of a data table, and then compile the results as sums for each value in value_range.</p>
  
  <p>The METHOD #1 below shows best what I am trying to do. The loop of 10 times is only there to compare the system.time it takes.</p>
  
  <pre><code>nb_values = 300
  value_range = c(1:nb_values)
  
  nb_rows = 1000
  d = data.table( c1 = rep(c(10,30,60,80), (nb_rows/4)) , c2 = rep(2,nb_rows) )
  
  res1 = data.table( rep(0,nb_values) )
  res2 = data.table( rep(0,nb_values) )
  
  # METHOD 1
  system.time(
      for(i in (1:10))
      {
          for(i in value_range) res1[i] &lt;- d[, sum( (i&gt;c1) &amp; (i&gt;c2) ),]
      }
  )
  #   user  system elapsed 
  #  6.973   0.000   6.982 
  
  # METHOD 2
  f &lt;- function(i)
  {
      d[, sum( (i&gt;c1) &amp; (i&gt;c2) ),]
  }
  
  system.time(
      for(i in (1:10))
      {
          res2 &lt;- apply(cbind(value_range), 1, f)
      }
  )
  #    user  system elapsed 
  #   3.152   0.000   3.161 
  
  identical(as.integer(res1$V1), res2)
  # [1] TRUE
  </code></pre>
  
  <p>Is there a way to speed this up even further ?</p>