title: Partial string substitution for row names in a data frame
body: <p>My question is more about improving my coding skills than about solving a problem as I was able to figure out the solution but I don't find it very elegant. </p>
  
  <p>I am working on a bit more complex version of the one posted <a href="https://stackoverflow.com/questions/26866839/how-to-export-coefficients-of-the-regression-analysis-from-rstudio-to-a-spreadsh">here</a>. I am running multiple linear regressions and I want to export the coefficients from all of them to a single csv file. I was able to use <a href="https://stackoverflow.com/questions/18474336/automated-formula-construction">this</a> information to generate the list of all coefficients and to convert it to a list of data frame. My list of data frames looks like this:</p>
  
  <pre><code>&gt; coef.df
  [[1]]
                      Estimate Std. Error    z value     Pr(&gt;|z|)
  (Intercept)      -0.08670899   0.357377 -0.2426261 0.8082950694
  Var.0.0.Type.4   22.46262205   5.935317  3.7845698 0.0001539747
  
  [[2]]
                     Estimate Std. Error    z value     Pr(&gt;|z|)
  (Intercept)      -0.1682616  0.3590799 -0.4685911 6.393619e-01
  Var.0.5.Type.4   15.4974199  3.8693290  4.0051957 6.196616e-05
  
  [[3]]
                     Estimate Std. Error    z value     Pr(&gt;|z|)
  (Intercept)      -0.1832488  0.3532577 -0.5187397 6.039423e-01
  Var.1.0.Type.4   10.1225605  2.4475064  4.1358668 3.536172e-05
  </code></pre>
  
  <p>and so on.</p>
  
  <p>When I tried to simply convert this list to a csv file I got messed up column names (all "Intercept" terms had a number added).</p>
  
  <pre><code>                   Estimate Std. Error     z value     Pr(&gt;|z|)
  (Intercept)      -0.08670899  0.3573770 -0.24262609 8.082951e-01
  Deg.In.0.0.INS.4 22.46262205  5.9353171  3.78456983 1.539747e-04
  (Intercept)1     -0.16826164  0.3590799 -0.46859114 6.393619e-01
  Deg.In.0.5.INS.4 15.49741993  3.8693290  4.00519568 6.196616e-05
  (Intercept)2     -0.18324877  0.3532577 -0.51873968 6.039423e-01
  Deg.In.1.0.INS.4 10.12256045  2.4475064  4.13586682 3.536172e-05
  (Intercept)3     -0.14188918  0.3426645 -0.41407607 6.788184e-01
  Deg.In.1.5.INS.4  6.32348365  1.5164421  4.16994719 3.046702e-05
  </code></pre>
  
  <p>I understand that rows have to have unique names and I want to customize them using the name of a second coefficient for each model. What I would like to do is to create a csv file that will have all the information in a following format with the row names adjusted to account for which variable a given Intercept is:</p>
  
  <pre><code>                          Estimate Std. Error    z value     Pr(&gt;|z|)
  (Intercept.0.0.Type.4)   -0.0867089   0.357377  -0.2426261 0.8082950694
  Var.0.0.Type.4           22.4626220   5.935317   3.7845698 0.0001539747
  (Intercept.0.5.Type.4)   -0.1682616   0.359079  -0.4685911 6.393619e-01
  Var.0.5.Type.4           15.4974199   3.869329   4.0051957 6.196616e-05
  (Intercept.1.0.Type.4)   -0.1832488   0.353257  -0.5187397 6.039423e-01
  Var.1.0.Type.4           10.1225605   2.447506   4.1358668 3.536172e-05
  </code></pre>
  
  <p>I don't have much experience manipulating partial string substitution and while I was able to do so but I think my code is not the most straight forward. Here is how I was able to obtain this result:</p>
  
  <pre><code>#I created a vector containing all row names
  df.names &lt;- unlist(lapply(coef.df,rownames)) 
  &gt; df.names
   [1] "(Intercept)" "Var.0.0.INS.4" "(Intercept)" "Var.0.5.INS.4" 
   [5] "(Intercept)" "Var.1.0.INS.4" "(Intercept)" "Var.1.5.INS.4" 
   [9] "(Intercept)" "Var.0.0.INS.5" "(Intercept)" "Var.0.5.INS.5"
  [13] "(Intercept)" "Var.1.0.INS.5" "(Intercept)" "Var.1.5.INS.5"
  #I created a vector with all "(Intercept)" elements from df.names
  inter.lm &lt;- df.names[c(TRUE, FALSE)] 
  &gt; inter.lm
  [1] "(Intercept)" "(Intercept)" "(Intercept)" "(Intercept)" "(Intercept)"
  [6] "(Intercept)" "(Intercept)" "(Intercept)"
  #I created a vector with all remaining elements from df.names 
  var.lm &lt;- df.names[c(FALSE,TRUE)] coefficients
  &gt; var.lm
  [1] "Var.0.0.Type.4" "Var.0.5.Type.4" "Var.1.0.Type.4" "Var.1.5.Type.4" 
  [5] "Var.0.0.Type.5" "Var.0.5.Type.5" "Var.1.0.Type.5" "Var.1.5.Type.5"
  #I removed the "Var" part from all elements in var.lm
  var.temp &lt;- gsub("Var(.*)", "\\1", var.lm)
  &gt; var.temp
  [1] ".0.0.Type.4" ".0.5.Type.4" ".1.0.Type.4" ".1.5.Type.4" ".0.0.Type.5"
  [6] ".0.5.Type.5" ".1.0.Type.5" ".1.5.Type.5"
  #I removed the ")" part from all elements in inter.lm
  inter.temp &lt;- gsub("\\)", "", inter.lm) 
  &gt; inter.temp
  [1] "(Intercept" "(Intercept" "(Intercept" "(Intercept" "(Intercept"
  [6] "(Intercept" "(Intercept" "(Intercept"
  #I pasted together vectors inter.tepm and var.temp to get the required names 
  inter.new &lt;- paste(inter.temp,var.temp,")",sep="")
  &gt; inter.new
  [1] "(Intercept.0.0.Type.4)" "(Intercept.0.5.Type.4)" "(Intercept.1.0.Type.4)"   
  [4] "(Intercept.1.5.Type.4)" "(Intercept.0.0.Type.5)" "(Intercept.0.5.Type.5)"
  [7] "(Intercept.1.0.Type.5)" "(Intercept.1.5.Type.5)"
  #I merged the inter.new and var.lm vectors to get the correct naming
  df.names &lt;- c(rbind(inter.new, var.lm))
  &gt; df.names
   [1] "(Intercept.0.0.Type.4)" "Deg.In.0.0.Type.4"     
   [3] "(Intercept.0.5.Type.4)" "Deg.In.0.5.Type.4"     
   [5] "(Intercept.1.0.Type.4)" "Deg.In.1.0.Type.4"     
   [7] "(Intercept.1.5.Type.4)" "Deg.In.1.5.Type.4"  
   [9] "(Intercept.0.0.INS.5)" "Deg.In.0.0.INS.5"     
  [11] "(Intercept.0.5.INS.5)" "Deg.In.0.5.INS.5"     
  [13] "(Intercept.1.0.INS.5)" "Deg.In.1.0.INS.5"     
  [15] "(Intercept.1.5.INS.5)" "Deg.In.1.5.INS.5"     
  #Finally I changed the row names
  rownames(final.df) &lt;- df.names
  </code></pre>
  
  <p>Is there an easier/shorter way to get the names I want?</p>