title: Find nearest highest value or equivalent based on multiple variables
body: <p>I have the following three dataframes:</p>
  
  <pre><code>df1
     grade PIR  rate
  1      7 min 10.80
  2      8 min 11.26
  3      9 min 12.10
  4      7  X1 11.17
  5      8  X1 11.65
  6      9  X1 12.56
  7      7  X2 11.55
  8      8  X2 12.06
  9      9  X2 13.03
  10     7  X3 11.95
  ...
  </code></pre>
  
  <p>and</p>
  
  <pre><code>df2
     grade PIR new_rate
  1      7 min    13.00
  2      8 min    13.00
  3      9 min    13.00
  4      7  X1    13.48
  5      8  X1    13.48
  6      9  X1    13.48
  7      7  X2    13.98
  8      8  X2    13.98
  9      9  X2    13.98
  10     7  X3    14.50
  ...
  </code></pre>
  
  <p>and</p>
  
  <pre><code>df3
  ID      assoc_rate   assoc_grade
  124575  10.80        7
  123413  11.42        7
  111539  11.65        8
  112284  12.04        8
  125245  12.10        9
  132588  12.44        9
  ....
  </code></pre>
  
  <p>For each case of <code>df3$assoc_rate</code> and <code>df3$assoc_grade</code> I need to identify the <strong>nearest highest value (or equivalent)</strong> of  <code>df1$rate</code> <em>within the same <code>df1$grade</code></em>, in order to join the <code>df1$grade</code> and <code>df1$PIR</code>, which would then allow me to attach the  <code>df2$new_rate</code> based on <code>grade</code> and <code>PIR</code>. </p>
  
  <p>My end goal is to attach the <code>new_rate</code> and <code>PIR</code> from <code>df2</code> to <code>df3</code> based on their <code>assoc_rate</code> and <code>assoc_grade</code>, but it has to be in relation to <code>df1</code>.</p>
  
  <p>So my desired dataframe would look like:</p>
  
  <pre><code>df_desired
  ID      assoc_rate   assoc_grade   PIR   new_rate 
  124575  10.80        7             min     13.00      
  123413  11.42        7             X2      13.98     
  111539  11.65        8             X1      13.48     
  112284  12.04        8             X2      13.98     
  125245  12.10        9             min     13.00     
  132588  12.44        9             X1      13.48 
  ....
  
  Here are the dataframes:
  df1 &lt;- data.frame(grade = c(7L, 8L, 9L, 7L, 8L, 9L, 7L, 8L, 9L,7L, 8L, 9L, 7L, 8L, 9L), PIR = c("min", "min", "min", "X1", "X1","X1", "X2", "X2", "X2", "X3", "X3", "X3", "X4", "X4", "X4"),rate = c(10.8, 11.26, 12.1, 11.17, 11.65, 12.56, 11.55, 12.06, 13.03, 11.95, 12.49, 13.53, 12.35, 12.93, 14.04))
  df2 &lt;- data.frame(grade = c(7L, 8L, 9L, 7L, 8L, 9L, 7L, 8L, 9L,7L, 8L, 9L, 7L, 8L, 9L), PIR = c("min", "min", "min", "X1", "X1","X1", "X2", "X2", "X2", "X3", "X3", "X3", "X4", "X4", "X4"),new_rate = c(13, 13, 13, 13.48, 13.48, 13.48, 13.98, 13.98,13.98, 14.5, 14.5, 14.5, 15.04, 15.04, 15.04))
  df3 &lt;- data.frame(ID = c(124575, 123413, 111539, 112284, 125245, 132588), assoc_rate = c(10.80,11.42,11.65,12.04,12.10,12.44), assoc_grade = c(7,7,8,8,9,9))
  </code></pre>
  
  <p>Thank you for any insight or instruction. (I tried something like <a href="https://stackoverflow.com/questions/51413210/how-to-find-nearest-highest-value-when-join-data-table">this</a> but didn't know how to make it work.)</p>