title: How to split a string in r by a delimiter and discard the last two items?
body: <p>I have a string separated by <code>_</code> and I want to get rid of the last two elements. For example, from <code>A_B_C_D</code> I want to return <code>A_B</code>, and from <code>A_B_C_D_E</code> I want <code>A_B_C</code>. I have tried <code>str_split_fixed</code> from <code>stringr</code>:</p>
  
  <pre><code>my_string &lt;- "A_B_C_D"
  x &lt;- str_split_fixed(my_string,"_",3) 
  </code></pre>
  
  <p>but it returns <code>"A"  "B"  "C_D"</code> instead of  <code>"A_B" "C" "D"</code>, otherwise I could have done <code>head(x,-2)</code> to get <code>A_B</code></p>
  
  <p>Is there a better way than </p>
  
  <pre><code>paste(head(unlist(strsplit(my_string,"_")),-2),collapse="_")
  </code></pre>