title: inserting an underscore into a particular part of variable names R
body: <p>I'd like to insert an underscore after the first three characters of all variable names in a data frame. Any help would be much appreciated. </p>
  
  <p>Current data frame:</p>
  
  <pre><code>df1 &lt;- data.frame("genCrc_b1"=c(1,1,1),"genprd"=c(1,1,1) ,"genopr_b1_b2"=c(1,1,1))
  </code></pre>
  
  <p>Desired data frame:</p>
  
  <pre><code>df2 &lt;- data.frame("gen_Crc_b1"=c(1,1,1),"gen_prd"=c(1,1,1) ,"gen_opr_b1_b2"=c(1,1,1))
  </code></pre>
  
  <p>My attempts: </p>
  
  <pre><code>gsub('^(.{3})(.*)$', "_", names(df1))
  gsub('^(.{3})(.*)$', '\\_\\2', names(df1))
  </code></pre>