title: Data summary based on multiple categorical variables
body: <p>I'm trying to create a summary based on a data that contains the following columns:</p>
  
  <pre><code>    Trx_Date   Brand   Cust_Num   Item_Qty   Item_Price
  </code></pre>
  
  <p>I am trying to create a summary of the Item_Qty and Item_Amt based each week of the year for different Brand (a character object class). I have managed to create Wk_Num by:</p>
  
  <pre><code>    Wk_Num &lt;- as.character(strftime(as.POSIXlt(Trx_Date), format="%W"))
  </code></pre>
  
  <p>What I am trying to do is to get the sum of the Item_Qty and the mean of the Item_Price for each Wk_Num+Brand combination. I manage to get what I want through the following:</p>
  
  <pre><code>   tblsum &lt;- summary(Item_Price + Item_Qty ~ Wk_Num + Brand, data=tblorig, FUN = function(x) { c(m = mean(x), s= sum(x))})
  </code></pre>
  
  <p>What I want to do is to create another column which calculates the percentage of the buyers of all the total buyers (i.e. penetration) for each particular Wk_Num+Brand combination. I can revise the code above to calculate the length as well (to get the number of "buyers" for each combination), i.e.</p>
  
  <pre><code>   tblsum &lt;- summary(Item_Price + Item_Qty ~ Wk_Num + Brand, data=tblorig, FUN = function(x) { c(m = mean(x), s= sum(x), l=length(x))})
  </code></pre>
  
  <p>However, this is flawed as well, as customers may actually purchase multiple times within a week and they will be double counted.</p>
  
  <p>I'm still way early in my <strong>R</strong> journey and trying to code it elegantly. Is there a good way of combining the data summary that I build from the first code as well as calculate the % of unique Cust_Num for each Wk_Num+Brand combination over the total number of unique Cust_Num?</p>
  
  <p>Any improvement of the code would be greatly appreciated as well.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Sample data:</p>
  
  <pre><code> Wk_Num   Brand      Cust_Num   Item_Qty   Item_Price
   11       AAA           001          1          2.1
   11       BBB           001          1          1.4
   11       AAA           002          2          2.1
   12       CCC           003          1          1.5
   12       BBB           001          3          1.4
   12       BBB           001          2          1.4
   12       BBB           004          1          1.5
   12       CCC           004          1          1.5
   13       AAA           002          2          2.2
   13       AAA           001          3          2.1
   13       AAA           003          1          2.2
   13       AAA           004          2          2.1
  </code></pre>
  
  <p>What would be ideal as the output is:</p>
  
  <pre><code> Wk_Num   Brand     Total Item  Avg Item Price   Penetration
   11       AAA             3        2.10              50%         # 2 out of 4
   11       BBB             1        1.40              25%         # 1 out of 4
   12       BBB             6        1.43              50%         # 2 out of 4 (Cust 001 bought twice in that week)
   12       CCC             1        1.50              25%         # 1 out of 4
   13       AAA             8        2.15             100%         # 4 out of 4
  </code></pre>