title: R Summary based on column name length
body: <p>I have the following problem:
  I have a matrix with 80 columns which names have either 10/11, 21/22,31/32 or 42/43 characters. The names are totally different but the lenth fits always in one of the four groups. Now I would like to add four columns were I get the sum of all the values of columns corresponding to one group. Here is a little example of what I mean</p>
  
  <pre><code>a&lt;-rnorm(1:100)
  b&lt;-rnorm(1:100)
  cc&lt;-rnorm(1:100)
  dd&lt;-rnorm(1:100)
  eee&lt;-rnorm(1:100)
  fff&lt;-rnorm(1:100)
  g&lt;-data.frame(a,b,cc,dd,eee,fff)
  g$group1&lt;-"sum of all columns of with headers of length 1 (in this case a+b)"
  g$group2&lt;-"sum of all columns of with headers of length 2 (in this case cc+dd)"
  g$group3&lt;-"sum of all columns of with headers of length 3 (in this case eee+fff)"
  </code></pre>
  
  <p>I was able to transfer the matrix to a dataframe using melt() and carrying out the operation  using stringr::str_length(). However, I could not transform this back to a matrix which I really need as final output. The columns are not in order and ordering would not help me much, since the number of columns depends on the outcome of the previous calculation and it would be too tedious to define dataframe ranges every time again.
  Hope you can help.</p>