title: How to create matrix of distribution in R
body: <p>I have the following dataframe in R.</p>
  <pre><code>ID         Date                  List             Type
  P-10012    2020-04-15 12:13:15   ABC,ABD,BCD      TR1
  P-10012    2020-04-15 12:13:15   ABC,ABD,BCD      RES
  P-10012    2020-04-15 12:13:15   ABC,ABD,BCD      FTT
  P-10013    2020-04-12 17:10:05                    TR1
  P-10013    2020-04-12 17:10:05                    FTT
  P-10013    2020-04-12 17:10:05                    ZXR
  P-10014    2020-04-10 04:30:19   ABD,BCD          TR1
  P-10014    2020-04-10 04:30:19   ABD,BCD          ZXR
  P-10015    2020-04-10 14:13:15   ABC              
  P-10016    2020-04-10 13:13:15   
  P-10017    2020-03-18 10:13:15   ABC,ABD,BCD      TR1
  
  
  
  dput(df)
  df &lt;- structure(list(ID = c(&quot;P-10012&quot;, &quot;P-10012&quot;, 
  &quot;P-10012&quot;, &quot;P-10013&quot;, &quot;P-10013&quot;, &quot;P-10013&quot;, 
  &quot;P-10014&quot;, &quot;P-10014&quot;, &quot;P-10015&quot;, &quot;P-10016&quot;, 
  &quot;P-10017&quot;), Date = c(&quot;2020-04-15 12:13:15&quot;, &quot;2020-04-15 12:13:15&quot;, 
  &quot;2020-04-15 12:13:15&quot;, &quot;2020-04-12 17:10:05&quot;, &quot;2020-04-12 17:10:05&quot;, 
  &quot;2020-04-12 17:10:05&quot;, &quot;2020-04-10 04:30:19&quot;, &quot;2020-04-10 04:30:19&quot;, 
  &quot;2020-04-10 14:13:15&quot;, &quot;2020-04-10 13:13:15&quot;, &quot;2020-03-18 10:13:15&quot;
  ), Type = c(&quot;TR1&quot;, &quot;RES&quot;, &quot;FTT&quot;, &quot;TR1&quot;, &quot;FTT&quot;, &quot;ZXR&quot;, &quot;TR1&quot;, &quot;ZXR&quot;, NA, NA, &quot;TR1&quot;), List = c(&quot;ABC,ABD,BCD&quot;, &quot;ABC,ABD,BCD&quot;, &quot;ABC,ABD,BCD&quot;, 
  &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;ABD,BCD&quot;, &quot;ABD,BCD&quot;, &quot;ABC&quot;, &quot;&quot;, &quot;ABC,ABD,BCD&quot;)), class = &quot;data.frame&quot;, row.names = c(NA, 
  -11L))
  </code></pre>
  <p>The structure of the dataframe is that it will always have the same <code>List</code> value for a particular <code>ID</code> in case if there are multiple row available for that particular <code>ID</code> because it has multiple different value in <code>Type</code>. If for a particular <code>ID</code> there is only 1 <code>Type</code> value then it will always have one row.</p>
  <p>I need to create the following distribution for the month of <code>Apr-20</code> of <code>List</code> values as mentioned in the comma separated manner and <code>Type</code> values.</p>
  <p>Where, the first 7 rows in my <code>Required Df</code> are distinct count of <code>ID</code> based on the condition (i.e whether <code>List</code> or <code>Type</code> are blank or not) and distribution across all unique <code>List</code> and <code>Type</code> value. For these 7 rows the <code>Distinct_Count</code> should be divided by <code>Total</code> to get <code>Percentage</code>. However, from 8th row and onward if the unique value is form <code>List</code> then it should be divided by total distinct count of <code>Non_Blank_List</code> and if the value is from <code>Type</code> then it should be divided by total distinct count of <code>Non_Blank_Type</code>.</p>
  <p>In the following matrix, I just want to understand what is the distribution of unique values of <code>List</code> and <code>Type</code> distinctly and in combination with other values.</p>
  <p>Please not that for the example purpose I have simplified the <code>List</code> and <code>Type</code> values in 3 and 4 unique values respectively but in my actual dataframe it is quite high and it varies from month to month so please don't hard code the values.</p>
  <p>I have tried multiple approach but couldn't achieve the required output yet.</p>
  <p>Required Df&lt;-</p>
  <pre><code>APR-21           Distinct_Count    Percentage    ABC     ABD      BCD     TR1    RES    FTT     ZXR
  Total_ID         5                 100.00%       2       2        2       3      1      2       2 
  Blank_List       2                 40.00%        0       0        0       1      0      1       1
  Blank_Type       2                 40.00%        1       0        0       0      0      0       0
  Both_Blank       1                 20.00%        0       0        0       0      0      0       0
  Non_Blank_List   3                 60.00%        2       2        2       2      1      1       1         
  Non_Blank_Type   3                 60.00%        1       2        2       3      1      1       2
  Both_Non_Blank   2                 40.00%        1       2        2       2      1      1       1
  ABC              1                 33.33%        2       1        1       1      1      1       0
  ABD              0                  0.00%        1       2        2       2      1      1       1
  BCD              0                  0.00%        1       2        2       2      1      1       1
  TR1              0                  0.00%        1       2        2       3      1      1       1 
  RES              0                  0.00%        1       1        1       1      1      1       0    
  FTT              0                  0.00%        1       1        1       2      1      2       1 
  ZXR              0                  0.00%        0       1        1       1      0      1       2
  </code></pre>