title: How to filter a dataframe based on values in another dataframe in R
body: <p>I have two dataframes, but I'm having trouble getting one to filter based on values in the other.</p>
  <p>Example:</p>
  <p>df1 has a bunch of columns, but column A is the one that matters because it corresponds to values in df2. Column A looks something like this:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>A</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  </tr>
  <tr>
  <td>5</td>
  </tr>
  <tr>
  <td>1</td>
  </tr>
  <tr>
  <td>5</td>
  </tr>
  <tr>
  <td>1</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>df2 looks something like this:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>A</th>
  <th>B</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>0.92</td>
  </tr>
  <tr>
  <td>5</td>
  <td>0.02</td>
  </tr>
  <tr>
  <td>1</td>
  <td>0.18</td>
  </tr>
  <tr>
  <td>5</td>
  <td>0.87</td>
  </tr>
  <tr>
  <td>1</td>
  <td>0.46</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>For the A value in each row in df1, I want to find the number of times B (in df2) is above 0.5 divided by the number of times the A value for that row appears in df2. So in df1, I'd like a new column called C, which would look like this in this example:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>C</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>0.33</td>
  </tr>
  <tr>
  <td>0.5</td>
  </tr>
  <tr>
  <td>0.33</td>
  </tr>
  <tr>
  <td>0.5</td>
  </tr>
  <tr>
  <td>0.33</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>My code to attempt this is as follows:</p>
  <pre><code>df1$C &lt;- (df2 %&gt;% filter(A == df1$A &amp; B &gt; 0.5) %&gt;% summarize(n()))/(df2 %&gt;% filter(A == df1$A) %&gt;% summarize(n()))
  </code></pre>
  <p>If I try this but replace <code>df1$A</code> with some arbitrary number instead of the reference, it works fine. However, when I try to use the reference for each individual row in df1, I get an error message.</p>