title: R: Fast hash search in lists (environment)
body: <p>I want to have a very quick search and it seems, that using hashes (via environments) is the best way. Now, I got an example to run with environments, but it does not return what I need.</p>
  
  <p>Here is an example:</p>
  
  <pre><code>a &lt;- data.table::data.table(a=c(1, 3, 5), b=c(2, 4, 6), time=c(10, 20, 30))  
  my_env &lt;- list2env(a)  
  x &lt;- a[2, .(a, b)] # x=c(3,4)  
  found &lt;- get("x", envir = my_env)  
  </code></pre>
  
  <p>I would expect <code>found = c(3, 4, 20)</code> but receive <code>found = c(3, 4)</code>
  (I want the whole row to be returned instead of the unknown row subset)</p>
  
  <p><strong>Backround</strong>: I have a huge list containing source and destination of routes calculated with osrm, e.g.</p>
  
  <pre><code>lattitude1, longitude1, lattitude2, longitude2, travel-time  
  46.12, 8.32, 47.87, 9.92, 1036  
  ...  
  </code></pre>
  
  <p>The list contains in a first example about 100000 rows. Using binary search in a data.table speeded up my code by a factor 100, but one search still takes 1 ms. As I have to search for many routes during a simulation (About 2e5 searches) I would like to get even faster.<br>
  <strong>@Gregor</strong>: I am a beginner in R, but I don't think my question is a duplicate: </p>
  
  <ol>
  <li>I knew the second link , which is an abstract overview for experts listing possibilities. Furthermore, it is 4 years old. </li>
  <li>I didn't know the first link, but from those answers I can't see whether I should switch to environments and how an implementation could work at all. There is also no discussion about searching a part of a huge list.</li>
  </ol>
  
  <p><strong>Summary (Thanks to DigEmAll for his running example below)</strong>:</p>
  
  <ul>
  <li>Using Rcpp on integers, the search is less memory consuming without any loss of quality. Futhermore, it is about a factor of 3 faster.</li>
  <li>Do not use hashed environments when you want to look up doubles (which have to be converted to strings).</li>
  <li>Implementation in existing code should be easy.</li>
  </ul>