title: Extract string from columns and paste it in the column values in R
body: <p>This is an example of my initial table:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>A35.Factor1</th>
  <th>A36.Factor2</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>level1</td>
  <td>level3</td>
  </tr>
  <tr>
  <td>level2</td>
  <td>level4</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>My desired output is:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Factor1</th>
  <th>Factor2</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>A35.level1</td>
  <td>A36.level3</td>
  </tr>
  <tr>
  <td>A35.level2</td>
  <td>A36. level4</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>My guess was to iterate through each column, extract the string through a regular expression and paste it using mutate() to the existing column, something like:</p>
  <pre><code>for(i in 1:ncol(dataset)) {     
    code &lt;- colnames(dataset[,i])
    code &lt;- str_extract(code, &quot;A[:digit:]&quot;)
    dataset[ , i] &lt;- dataset[, i] %&gt;% mutate(dataset[,I] = paste(dataset[,i], code))
  }
  </code></pre>
  <p>But it is not working, I guess I am reasoning in a bad way...
  thanks in advance!</p>