title: Sum a set of numeric columns and collapse string column by group
body: <p>Suppose I have a data frame (df) like this:</p>
  <pre><code>    Names ID Thing1 Thing2 Thing3 Thing4 Thing5
  1:  Gen1 id1    10     5    10     5      10
  2:  Gen2 id2     1     2     3     4       5
  3:  Gen1 id3    10     5    10     5      10
  4:  Gen2 id4     1     2     3     4       5
  5:  Gen3 id5     7     7     7     7       7
  </code></pre>
  <p>For each 'Names', I would like to sum 'Thing' columns, and collapse the strings in 'ID':</p>
  <pre><code>   Names   ID     Thing1  Thing2 Thing3 Thing4 Thing5
  1:  Gen1 id1|id3    20      10     20     10    20
  2:  Gen2 id2|id4     2       4      6      8    10
  3:  Gen3 id5         7       7      7      7     7
  </code></pre>
  <p>I am able to achieve this via dplyr:</p>
  <pre><code>df1 &lt;- df %&gt;%
          group_by(Names)%&gt;%
          summarise_each(funs(paste(unique(.), collapse='|')),matches('^\\D+$'))
  
  
  df2 &lt;- df %&gt;%
           group_by(Names)%&gt;%
             summarise_each(funs(sum = sum(., na.rm=TRUE)), starts_with('Thing' )) 
  
  bind_cols(df1, df2[-1]) 
  </code></pre>
  <p>However, this solution takes very long since I have a data frame with more than 10k rows and more than 10k column!</p>
  <p>Is there any possible solution with <code>data.table</code>?</p>
  <p>The closest I have gotten is this here:</p>
  <pre><code>&gt; setDT(df)[, c(paste(df$ID,collapse = &quot;-&quot;, sep = &quot;&quot;), lapply(.SD, sum, na.rm = TRUE)), 
              by = Names, .SDcols = !&quot;ID&quot;]
  
     Names                     Thing1 Thing2 Thing3 Thing4 Thing5
  1:  Gen1 id1-id2-id3-id4-id5     20     10     20     10     20
  2:  Gen2 id1-id2-id3-id4-id5      2      4      6      8     10
  3:  Gen3 id1-id2-id3-id4-id5      7      7      7      7      7
  </code></pre>
  <p>Obviously this is not what I am going for since it will collapse all IDs and not just the ones that were aggregated by summarizing via &quot;Names&quot;.</p>
  <p>I would very much appreciate your help!</p>
  <p>Here is the example data:</p>
  <pre><code>df &lt;- structure(list(Names = c(&quot;Gen1&quot;, &quot;Gen2&quot;, &quot;Gen1&quot;, &quot;Gen2&quot;,&quot;Gen3&quot;),
                        ID=c(&quot;id1&quot;,&quot;id2&quot;,&quot;id3&quot;,&quot;id4&quot;,&quot;id5&quot;),
                        Thing1 = c(10L, 1L, 10L, 1L, 7L), 
                        Thing2 = c(5L, 2L, 5L, 2L,7L), 
                        Thing3 = c(10L, 3L, 10L, 3L, 7L), 
                        Thing4 = c(5L, 4L, 5L,4L, 7L), 
                        Thing5 = c(10L, 5L, 10L, 5L, 7L)), 
                        .Names = c(&quot;Names&quot;,&quot;ID&quot;,&quot;Thing1&quot;, &quot;Thing2&quot;, &quot;Thing3&quot;, &quot;Thing4&quot;, &quot;Thing5&quot;),
                        class = &quot;data.frame&quot;, row.names = c(1:5L))
  </code></pre>