title: Directly (re)naming summarized columns with data.table
body: <p>I want to check the proportions of certain categories inside columns of a data.table with respect to some grouping variable. A working chunk of code is attached that does just this:</p>
  <pre><code>require(data.table)
  
  n_categories &lt;- 5
  
  dt &lt;- data.table(categories = sample(paste(&quot;Category&quot;, 1:n_categories), 
                                       1000, 
                                       replace = TRUE), 
                   group_var = rep(1:10, 100))
  
  dt[, lapply(1:n_categories, function(x) table(categories)[[x]]/.N), by = group_var]
  </code></pre>
  <p>The following output is produced:</p>
  <pre><code>    group_var   V1   V2   V3   V4   V5
   1:         1 0.22 0.19 0.18 0.24 0.17
   2:         2 0.17 0.23 0.18 0.23 0.19
   3:         3 0.17 0.22 0.21 0.17 0.23
   4:         4 0.17 0.17 0.24 0.19 0.23
   5:         5 0.26 0.19 0.16 0.19 0.20
   6:         6 0.14 0.19 0.24 0.21 0.22
   7:         7 0.12 0.14 0.28 0.30 0.16
   8:         8 0.13 0.19 0.19 0.17 0.32
   9:         9 0.23 0.26 0.24 0.17 0.10
  10:        10 0.16 0.20 0.21 0.25 0.18
  </code></pre>
  <p>Now, I wonder if there is a way to modify the given code to directly assign column names (&quot;proportion_category_1&quot;, &quot;proportion_category_2&quot;, etc.) to the resulting data.table object other than assigning new column names in a new line of code.</p>
  <p>The resulting data.table object should look something like this:</p>
  <pre><code>    group_var proportion_category_1 proportion_category_2 proportion_category_3 proportion_category_4 proportion_category_5
   1:         1                  0.22                  0.19                  0.18                  0.24                  0.17
   2:         2                  0.17                  0.23                  0.18                  0.23                  0.19
   3:         3                  0.17                  0.22                  0.21                  0.17                  0.23
   4:         4                  0.17                  0.17                  0.24                  0.19                  0.23
   5:         5                  0.26                  0.19                  0.16                  0.19                  0.20
   6:         6                  0.14                  0.19                  0.24                  0.21                  0.22
   7:         7                  0.12                  0.14                  0.28                  0.30                  0.16
   8:         8                  0.13                  0.19                  0.19                  0.17                  0.32
   9:         9                  0.23                  0.26                  0.24                  0.17                  0.10
  10:        10                  0.16                  0.20                  0.21                  0.25                  0.18
  </code></pre>
  <p>The use of the <code>lapply</code> statement or some kind of routine that is able to group by an arbitrary number of categories is critical.</p>