title: Cast transactional data
body: <p>I need to <code>cast</code> my data:</p>
  
  <pre><code>&gt; data.frame(transactionID= c("941214", "941214", "140668","945320","945320","945320"), productCategory = c("A","B","C","B","C","A"))
    transactionID productCategory
  1        941214               A
  2        941214               B
  3        140668               C
  4        945320               B
  5        945320               C
  6        945320               A
  </code></pre>
  
  <p>into this aggregated form:</p>
  
  <pre><code>productCategory A.purchase B.purchase C.purchase
  A               2          2          1
  B               2          2          1
  C               1          1          2
  </code></pre>
  
  <p>At <code>A.purchase</code> column and <code>row.number = 1</code>, I need count transactions in which product A appears.
  At <code>B.purchase</code> column and <code>row.number = 1</code>, I need count transactions in which product B appears together with A.</p>
  
  <p>I tried this: <code>dcast(data, productCategory~productCategory)</code>. But it is not counting when product B is together with product A in a transaction.</p>
  
  <p><strong>SOLUTION:</strong></p>
  
  <pre><code>library(dplyr)
  library(reshape)
  library(tidyr)
  
  &gt; aux = data %&gt;%
       group_by(transactionID)%&gt;%
       tidyr::expand(nesting(productCategory), productCategory) 
  
  &gt; aux
     transactionID productCategory productCategory1
            &lt;fctr&gt;          &lt;fctr&gt;           &lt;fctr&gt;
   1        140668               C                A
   2        140668               C                B
   3        140668               C                C
   4        941214               A                A
   5        941214               A                B
   6        941214               A                C
   7        941214               B                A
   8        941214               B                B
   9        941214               B                C
  10        945320               A                A
  11        945320               A                B
  12        945320               A                C
  13        945320               B                A
  14        945320               B                B
  15        945320               B                C
  16        945320               C                A
  17        945320               C                B
  18        945320               C                C
  
  &gt; names(aux)[2:3] = c("productCategory1","productCategory")
  &gt; result = inner_join(aux, data) %&gt;% dcast(productCategory1~productCategory)
  
  
  &gt; result
    productCategory1 A B C
  1                A 2 2 1
  2                B 2 2 1
  3                C 1 1 2
  </code></pre>