title: remove everything after the last underscore of a column in R
body: <p>I have a dataframe and for a particular column I want to strip out everything after the last underscore.</p>
  
  <p>So:</p>
  
  <pre><code>test &lt;- data.frame(label=c('test_test_test', 'test_tom_cat', 'tset_eat_food', 'tisk - tisk'), 
                     stuff=c('blah', 'blag', 'gah', 'nah') , 
                     numbers=c(1,2,3, 4))
  </code></pre>
  
  <p>should become</p>
  
  <pre><code>result &lt;- data.frame(label=c('test_test', 'test_tom', 'tset_eat', 'tisk - tisk'), 
                     stuff=c('blah', 'blag', 'gah', 'nah') , 
                     numbers=c(1,2,3, 4))
  </code></pre>
  
  <p>I have got:</p>
  
  <pre><code>require(dplyr)
  test %&gt;%
    mutate(label = gsub('_.*','',label))
  </code></pre>
  
  <p>but that drops everything from the first underscore and gives me</p>
  
  <pre><code> wrong_result &lt;- data.frame(label=c('test', 'test', 'tset', 'tisk - tisk'), 
                     stuff=c('blah', 'blag', 'gah', 'nah') , 
                     numbers=c(1,2,3, 4))
  </code></pre>