title: R: Check if value from dataframe is within range other dataframe
body: <p>I am looking for a way to look up infromation from 1 dataframe in another dataframe, get a value from that other dataframe and pass it back to the first frame..</p>
  
  <p>example data:</p>
  
  <p>I've got a dataframe named "x"</p>
  
  <pre><code>x &lt;- structure(list(from = c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L
  ), to = c(2L, 3L, 4L, 5L, 6L, 2L, 3L, 4L, 5L, 6L), number = c(30, 
  30, 30, 33, 34, 35, 36, 37, 38, 39), name = c("region 1", "region 2", 
  "region 3", "region 4", "region 5", "region 6", "region 7", "region 8", 
  "region 9", "region 10")), .Names = c("from", "to", "number", 
  "name"), row.names = c(NA, -10L), class = "data.frame")
  
  #   from to number      name
  #1     1  2     30  region 1
  #2     2  3     30  region 2
  #3     3  4     30  region 3
  #4     4  5     33  region 4
  #5     5  6     34  region 5
  #6     1  2     35  region 6
  #7     2  3     36  region 7
  #8     3  4     37  region 8
  #9     4  5     38  region 9
  #10    5  6     39 region 10
  </code></pre>
  
  <p>This dataframe holds information about certain regions (1-10)</p>
  
  <p>I've got another dataframe "y"</p>
  
  <pre><code>y &lt;- structure(list(location = c(1.5, 2.8, 10, 3.5, 2), id_number = 
  c(30, 30, 38, 40, 36)), .Names = c("location", "id_number"), row.names 
  = c(NA, -5L), class = "data.frame")
  
  #  location id_number
  #1      1.5        30
  #2      2.8        30
  #3     10.0        38
  #4      3.5        40
  #5      2.0        36
  </code></pre>
  
  <p>This one containt information about locations.</p>
  
  <p>What I need is a function (or command, or whatever I can throw at R ;-) ) that:
  for every row in y: looks up if the y$location fits between x$from and x$to AND y$id_number == x$number. 
  If a match is found (a location y can only fall in 1 row of x, or in 0. it is impossible for y to exist in two rows in y), return x$name to a new column in y, named "name</p>
  
  <p>desired output:</p>
  
  <pre><code>#  location id_number     name
  #1      1.5        30 region 1
  #2      2.8        30 region 2
  #3     10.0        38     &lt;NA&gt;
  #4      3.5        40     &lt;NA&gt;
  #5      2.0        36 region 7
  </code></pre>
  
  <p>I'm pretty new to R, so my first idea was to use for-loops to tackle this problem (as I'm used to do in VB). But then I thought: "noooooo", I have to verctorise it, like all the people are telling me good R-programmers do ;-)</p>
  
  <p>So I came up with a function, and called it with adply (from the plyr-package).
  Problem is: It does not work, throws me an error I don't understand, and now I'm stuck...</p>
  
  <p>Can anyone point me in the right direction?</p>
  
  <pre><code>require("dplyr")
  
  getValue &lt;- function(y, x) {
   tmp &lt;- x %&gt;%
     filter(from &lt;= y$location, to &gt; y$location, number == y$id_number)
   return(tmp$name)
  }
  
  y["name"] &lt;- adply(y, 1, getValue, x=x)
  </code></pre>