title: Replace some values in a dataframe if they are included in another dataframe with strings
body: <p>I want to create a variable (in this case &quot;level&quot;) in a data frame which categorizes the numeric value of another variable (&quot;classwork&quot;). The table below (the one consisting variables &quot;classwork&quot; and &quot;level&quot;) is just an illustration; it does not represent the real data. The real datasets are provided at the end of this question.</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>classwork</th>
  <th>level</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>Low</td>
  </tr>
  <tr>
  <td>1.5</td>
  <td>Low</td>
  </tr>
  <tr>
  <td>4</td>
  <td>High</td>
  </tr>
  <tr>
  <td>2</td>
  <td>Low</td>
  </tr>
  <tr>
  <td>5</td>
  <td>High</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I used Agglomerative Hierarchical Clustering to divide the &quot;classwork&quot; variable into two categories, i.e. &quot;High&quot; and &quot;Low&quot;. The values included in the &quot;High&quot; category is stored in the data frame &quot;group1&quot; and the &quot;Low&quot; category in the data frame &quot;group2&quot;. Therefore, I would like to request R:</p>
  <ol>
  <li><p>to insert the string &quot;High&quot; for any value in classwork found in group1, and</p>
  </li>
  <li><p>to insert the string &quot;Low&quot; for any value in classwork found in group2.</p>
  </li>
  </ol>
  <p>This is what I already have (but did not seem to work as I expected). I prefer for both conditions to be included in one functions, but I don't mind if two functions are necessary.</p>
  <pre><code>replace(DATA.RH.TAM$C.level, DATA.RH.TAM$classwork == group1.C, &quot;High&quot;)
  replace(DATA.RH.TAM$C.level, DATA.RH.TAM$classwork == group2.C, &quot;Low&quot;)
  </code></pre>
  <p>The followings are my datasets:</p>
  <pre><code>DATA.RH.TAM &lt;- structure(list(classwork = c(4.714, 3.714, 4.143, 2.857, 4.143, 
                               3.714, 3, 4, 3.429, 4.286, 3, 4.286, 3.571, 3.714, 3.714, 3.857, 
                               3.429, 3.714, 3.571, 3.571, 4.143, 4.429, 3.857, 3.714, 2.429, 
                               4.143, 4.286, 2.714, 3.143, 3.714, 3.857, 4.429, 4.571, 3.571, 
                               3.286, 4, 4, 4, 4.143, 3.286, 3, 3.286, 3.571, 3.857, 4.143, 
                               3.714, 4.286, 2.143, 4, 4, 1.571, 3.143, 3.571, 3.571, 4, 3.857, 
                               3.286, 3, 3.143, 3.286, 3.857, 4.143, 4, 3.143, 3.857, 3.857, 
                               4, 3.571, 4.571, 2.429, 3.429, 3.429, 3.429, 1.857, 3.571, 3, 
                               2.143, 3.714, 4.286, 3.286, 4.857, 4.286, 3.429, 3.143, 3.857, 
                               4.143, 4.286, 3.571, 3.429, 3.857, 3.571, 2.714, 3.714, 3, 3.857, 
                               5, 3.571, 4.714, 2.286, 2.429, 3.286, 4.429, 3, 2.429, 2.857, 
                               3.857, 3, 3.714, 2.286, 1.857, 3.714, 4.286, 4.143, 3.857, 4.143, 
                               3.857, 3.714, 2.143, 3.714, 1.429, 2.429, 3.857, 3, 2, 3, 3, 
                               3.714, 3, 4.143, 4, 4.429, 4.429, 4, 4, 4.429, 3.857, 3.571, 
                               3.571, 4.143, 3.429, 3.143, 5, 3.286, 3.571, 3.286, 3.857, 4.571, 
                               3, 2.714, 4, 2.429, 2.429, 4.429, 4.143), C.level = c(NA_real_, 
                                                                                     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                     NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_)), class = &quot;data.frame&quot;, row.names = c(NA, 
                                                                                                                                                                                       -154L))
  </code></pre>
  <pre><code>group1 &lt;- structure(list(classwork = c(4.714, 4.714, 4.857, 5, 5, 4.429, 
                               4.429, 4.429, 4.429, 4.429, 4.429, 4.429, 4.571, 4.571, 4.571, 
                               4.143, 4.143, 4.143, 4.143, 4.143, 4.143, 4.143, 4.143, 4.143, 
                               4.143, 4.143, 4.143, 4.143, 4.286, 4.286, 4.286, 4.286, 4.286, 
                               4.286, 4.286, 4.286, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3.857, 
                               3.857, 3.857, 3.857, 3.857, 3.857, 3.857, 3.857, 3.857, 3.857, 
                               3.857, 3.857, 3.857, 3.857, 3.857, 3.857, 3.857)), row.names = c(NA, 
                                                                                                -66L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))
  </code></pre>
  <pre><code>group2 &lt;- structure(list(classwork = c(3.714, 3.714, 3.714, 3.714, 3.714, 
                               3.714, 3.714, 3.714, 3.714, 3.714, 3.714, 3.714, 3.714, 3.714, 
                               3.714, 3.429, 3.429, 3.429, 3.429, 3.429, 3.429, 3.429, 3.429, 
                               3.571, 3.571, 3.571, 3.571, 3.571, 3.571, 3.571, 3.571, 3.571, 
                               3.571, 3.571, 3.571, 3.571, 3.571, 3.571, 2.857, 2.857, 2.714, 
                               2.714, 2.714, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3.143, 3.143, 
                               3.143, 3.143, 3.143, 3.143, 3.286, 3.286, 3.286, 3.286, 3.286, 
                               3.286, 3.286, 3.286, 3.286, 2.429, 2.429, 2.429, 2.429, 2.429, 
                               2.429, 2.429, 2.143, 2.143, 2.143, 2.286, 2.286, 1.571, 1.429, 
                               1.857, 1.857, 2)), row.names = c(NA, -88L), class = c(&quot;tbl_df&quot;, 
                                                                                     &quot;tbl&quot;, &quot;data.frame&quot;))
  </code></pre>
  <p>I hope that makes sense. Thank you very much for reading my question everyone.</p>
  <p>Best regards,</p>