title: Porportions by group in data.table
body: <p>After some time away from <code>R</code> I feel I am making very clumsy code to get basic summary statistics in <code>data.table</code>.</p>
  
  <p>What I am doing is finding proportion of individuals in good/bad health conditional on species.</p>
  
  <pre><code># Some data 
  n = 300
  set.seed(2)
  dt &lt;- data.table(type = sample(x = c("Dog", "Cat", "Horse"), size = n, replace = TRUE),
                   health = sample(x = c("Good", "Bad"), size = n, replace = TRUE))
  
  # Making the table. In a clumsy manner?
  dt.fr &lt;- dt[, .N, .(type, health)][, perc.type := N/sum(N)*100, 
                                     by = type][order(type, health)]
  dt.fr
  
      type health  N perc.type
  1:   Cat    Bad 38  44.70588
  2:   Cat   Good 47  55.29412
  3:   Dog    Bad 56  50.90909
  4:   Dog   Good 54  49.09091
  5: Horse    Bad 61  58.09524
  6: Horse   Good 44  41.90476
  </code></pre>
  
  <p>How would I produce the table above with more elegant code?</p>