title: A function that iterates through every row
body: <p>I'm writing a function but i'm getting slightly confused with the vector structure of R.</p>
  
  <p>I have a table of data where each person has a product and a product value. I have a table of tiers, where each product has tiers with a lower and upper bound, and then a return value. </p>
  
  <p>I want to do the following:</p>
  
  <ol>
  <li>Take the product from <code>data.person</code></li>
  <li>Look at just that product in <code>data.tiers</code></li>
  <li>Take the value from <code>data.person</code></li>
  <li>Compare it to the first tier. Return the value if it is in between
  the bottom and top tier.</li>
  <li>Do the same again for the next tier.</li>
  </ol>
  
  <p>Here's how far i've gotten:</p>
  
  <pre><code>Employee &lt;- c("Bob", "Dave", "Sarah")
  Product &lt;- c("A", "B", "A")
  Value &lt;- c(10, 20, 50)
  data.person &lt;- data.frame(Employee, Product, Value)
  
  Product &lt;- c("A", "A", "B", "B")
  Lower &lt;- c(0,20,0,10)
  Upper &lt;- c(20, 999, 10, 999)
  Return &lt;- c(0.05, .1, 0.04, .08)
  data.tiers &lt;- data.frame(Product, Lower, Upper, Return)
  
  myFunc &lt;- function(product, value)
  {
    tiers.temp &lt;- data.tiers %&gt;% 
      filter(Product == product )
  
    result &lt;- 0
  
    for (i in length(tiers.temp)){
  
      if(value &gt;tiers.temp$Lower[i] &amp; value &lt;= tiers.temp$Tier.Upper[i]){
        result &lt;- tiers.temp$Return[i] }
    }
  
    result
  
  }
  </code></pre>
  
  <p>My function takes the product vector and value vectors as inputs. I want to go in and filter the <code>data.tiers</code> table for the product on the current line in my data set. I'm not sure if this is the correct way to do this based on the vector nature of R.</p>
  
  <p>I then loop through the applicable tiers and perform my test and return a value which I need to assign to a column in the <code>data.person</code> table.</p>
  
  <p><strong>Expected Results:</strong> My final result is a vector of numbers from the <code>data.tiers</code> table. So the first line of the data set would return 0.05 as 10 is between 0 and 20.</p>
  
  <p>It would be good to get some info on best practices in a situation like this.</p>