title: R data.table: subgroup weighted percent of group
body: <p>I have a <code>data.table</code> like:</p>
  
  <pre><code>library(data.table)
  widgets &lt;- data.table(serial_no=1:100, 
                        color=rep_len(c("red","green","blue","black"),length.out=100),
                        style=rep_len(c("round","pointy","flat"),length.out=100),
                        weight=rep_len(1:5,length.out=100) )
  </code></pre>
  
  <p>Although I am not sure this is the most <code>data.table</code> way, I can calculate subgroup frequency by group using <code>table</code> and <code>length</code> in a single step-- for example, to answer the question "What percent of red widgets are round?"</p>
  
  <p><strong>edit:</strong> this code does not provide the right answer</p>
  
  <pre><code># example A
  widgets[, list(style = unique(style), 
                 style_pct_of_color_by_count = 
                   as.numeric(table(style)/length(style)) ), by=color]
  
  #    color  style style_pct_of_color_by_count
  # 1:   red  round                        0.32
  # 2:   red pointy                        0.32
  # 3:   red   flat                        0.36
  # 4: green pointy                        0.32
  # ...
  </code></pre>
  
  <p>But I can't use that approach to answer questions like "By weight, what percent of red widgets are round?" I can only come up with a two-step approach:</p>
  
  <pre><code># example B
  widgets[,list(cs_weight=sum(weight)),by=list(color,style)][,list(style, style_pct_of_color_by_weight=cs_weight/sum(cs_weight)),by=color]
  
  #    color  style style_pct_of_color_by_weight
  # 1:   red  round                    0.3466667
  # 2:   red pointy                    0.3466667
  # 3:   red   flat                    0.3066667
  # 4: green pointy                    0.3333333
  # ...
  </code></pre>
  
  <p>I'm looking for a single-step approach to B, and A if improvable, in an explanation that deepens my understanding of <code>data.table</code> syntax for by-group operations.  Please note that this question is different from <a href="https://stackoverflow.com/questions/17743745/weighted-sum-of-variables-by-groups-with-data-table">Weighted sum of variables by groups with data.table</a> because mine involves subgroups and avoiding multiple steps.  TYVM.</p>